简体   繁体   中英

pandas filter dates to last most recent 3 months

I want to filter a pandas data frame to the last most recent 3 months.

import pandas as pd
dates = pd.DataFrame(['2016-11-01', '2016-12-01', '2017-01-01', '2017-02-01', '2017-03-01'], columns=['date'])
dates.date = pd.DatetimeIndex(dates.date)
import datetime
today = datetime.date.today()
first = today.replace(day=1)
lastMonth = first - datetime.timedelta(days=90)
print (lastMonth.strftime("%Y-%m"))
dates[dates.date >= lastMonth]

This snippet sort of already works, but has the length of a month hard coded to 30 days. How can I use a pd.Timedelta('-3 month') (which does not seem to work like this) to achieve a more robust function?

I think you need offsets , because Timedelta only does not work with months:

lastMonth = first - pd.offsets.MonthBegin(3)
#lastMonth = first - pd.offsets.relativedelta(months=3)

lastMonth = first - pd.Timedelta(months=3)

ValueError: cannot construct a Timedelta from the passed arguments, allowed keywords are [weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM