1
我想過濾一個熊貓數據框到最近3個月。pandas過濾條件最近3個月
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]
這段代碼已經有效,但是有一個月的硬編碼爲30天。我如何使用pd.Timedelta('-3 month')
(看起來不像這樣工作)來實現更強大的功能?