In Pandas for Python, I have a data set that has a column of datetimes in it. I need to create a new column that has the date of the following Sunday for each row.
I've tried various methods trying to use iterrows and then figure out the day of the week, and add a day until the day is 7, but it hasn't worked and I'm not even sure how I'd return the date instead of just the day number then. I also don't feel like iterrows would be the best way to do it either.
What is the best way to return a column of the following Sunday from a date column?
Use the Pandas date offsets, e.g.:
>>> pd.to_datetime('2019-04-09') + pd.offsets.Week(n=0, weekday=6)
Timestamp('2019-04-14 00:00:00')
For example, this changes the provided datetime
over a week. This is vectorised, so you can run it against a series like so:
temp['sunday_dates'] = temp['our_dates'] + pd.offsets.Week(n=0, weekday=6)
our_dates random_data sunday_dates
0 2010-12-31 4012 2011-01-02
1 2007-12-31 3862 2008-01-06
2 2006-12-31 3831 2007-01-07
3 2011-12-31 3811 2012-01-01
N.b. Pass n=0
to keep a day, which is already on a Sunday, on that day. Pass n=1
if you want to force it to the next Sunday. The Week(weekday=INT)
parameter is 0 indexed on Monday and takes values from 0 to 6 (inclusive). Thus, passing 0 yields all Mondays, 1 yields all Tuesdays, etc. Using this, you can make everything any day of the week you would like.
N.b. If you want to go to the last Sunday, just swap +
to -
to go back.
N.b. (Such note, much bene) The specific documentation on time series functionality can be found here: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html