Reputation: 96294
Is there any way to get the date (a datetime
, pd.Timestamp
or equivalent) of the last day of this [week/month/quarter/year] with datetime
, pandas
or other date & time utils?
Upvotes: 8
Views: 9722
Reputation: 879651
You could add a pandas.DateOffset
to a DateTimeIndex
, Timestamp
or datetime.date
or datetime.datetime
:
dates = pd.date_range('2015-8-13', periods=4, freq='3D')
# DatetimeIndex(['2015-08-13', '2015-08-16', '2015-08-19', '2015-08-22'],
# dtype='datetime64[ns]', freq='3D', tz=None)
Snap to the last day of the week (for example, Sunday):
In [232]: dates+offsets.Week(weekday=6)
Out[232]: DatetimeIndex(['2015-08-16', '2015-08-23', '2015-08-23', '2015-08-23'], dtype='datetime64[ns]', freq=None, tz=None)
Snap to the last day of the month:
In [207]: dates+offsets.MonthEnd()
Out[207]: DatetimeIndex(['2015-08-31', '2015-08-31', '2015-08-31', '2015-08-31'], dtype='datetime64[ns]', freq=None, tz=None)
Snap to the last day in the quarter:
In [212]: dates+offsets.QuarterEnd()
Out[215]: DatetimeIndex(['2015-09-30', '2015-09-30', '2015-09-30', '2015-09-30'], dtype='datetime64[ns]', freq=None, tz=None)
Snap to the last day of the year:
In [219]: dates+offsets.YearEnd()
Out[222]: DatetimeIndex(['2015-12-31', '2015-12-31', '2015-12-31', '2015-12-31'], dtype='datetime64[ns]', freq=None, tz=None)
Notice that adding an offset always advances the date. For example, 2015-08-16 is a Sunday, and adding an offsets.Week(weekday=6)
advances it it 2015-08-23:
In [233]: pd.Timestamp('2015-8-16')+offsets.Week(weekday=6)
Out[233]: Timestamp('2015-08-23 00:00:00')
To prevent that from happening, you could subtract one day from dates
:
In [234]: dates - offsets.Day() + offsets.Week(weekday=6)
Out[237]: DatetimeIndex(['2015-08-16', '2015-08-16', '2015-08-23', '2015-08-23'], dtype='datetime64[ns]', freq=None, tz=None)
Upvotes: 5
Reputation: 2901
import datetime
from dateutil.relativedelta import relativedelta, SU
def last_day_of_week(year, week_num):
# assuming sunday is the last day of week
# weeks= is an offset, hence the minus one
return datetime.date(year, 1, 1) + relativedelta(weekday=SU(1), weeks=week_num - 1)
def last_day_of_month(year, month):
return datetime.date(year, month, 1) + relativedelta(months=1, days=-1)
def last_day_of_year(year):
return datetime.date(year, 1, 1) + relativedelta(years=1, days=-1)
Upvotes: 4
Reputation: 2472
Using datetime
only.
>>> d = datetime.date.today()
Last day of week:
# This was wrong earlier.
>>> d + datetime.timedelta(days=5 - d.weekday())
datetime.date(2015, 8, 15)
Last day of month:
>>> datetime.date(year=(d.year + int(d.month % 12 == 0)), month=(d.month + 1) % 12, day=1) - datetime.timedelta(days=1)
datetime.date(2015, 8, 31)
Last day of quarter:
>>> datetime.date(year=d.year, month=((d.month % 3) + 1) * 3 + 1, day=1) - datetime.timedelta(days=1)
datetime.date(2015, 9, 30)
Last day of year:
>>> datetime.date(year=d.year, month=12, day=31)
datetime.date(2015, 12, 31)
EDIT: This is all pretty ugly and using a higher level third party library is probably best, unless there is a compelling reason not to (and there does not seem to be here).
Upvotes: 7