月份是任意时间段,因为长度会变化使用布尔索引并针对当前日期减去 182 天进行过滤或者,relativedelta从 pythondateutil模块使用,它可以做monthsfrom datetime import datetimeimport pandas as pdfrom dateutil.relativedelta import relativedelta as rd# This line is just for creating test datadf = 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 daysdf_updated = df[df.datetime > datetime.today() - pd.Timedelta(days=182)]# alternatively, use the relativedeltadf_updated = df[df.datetime > datetime.today() - rd(months=6)]