1 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
月份是任意時(shí)間段,因?yàn)殚L(zhǎng)度會(huì)變化
使用布爾索引并針對(duì)當(dāng)前日期減去 182 天進(jìn)行過(guò)濾
或者,
relativedelta
從 pythondateutil
模塊使用,它可以做months
from datetime import datetime
import pandas as pd
from dateutil.relativedelta import relativedelta as rd
# This line is just for creating test data
df = pd.DataFrame({'datetime': pd.date_range(start='2020-01-01', end=datetime.today(), freq='1d').to_pydatetime().tolist()})
# filter out the everything greater than 182 days
df_updated = df[df.datetime > datetime.today() - pd.Timedelta(days=182)]
# alternatively, use the relativedelta
df_updated = df[df.datetime > datetime.today() - rd(months=6)]
添加回答
舉報(bào)