我把df1作为:
Acc_id Acc_name Acc_end_date
qqq-1 test1 12/31/2021
www-2 test2 05/28/2022
yyy-6 test3 06/15/2022
zzz-6 test4 06/17/2022
kkk-6 test5 03/16/2022Acc_end_date采用mm/dd/yy格式。
我只想得到那些帐户,其中Acc_end_date -今天的日期>30。
这意味着,从今天起30天后,Acc_id就会被选中。
我尝试了以下几点:
# changing the column to datetime format
df1['Acc_end_date']= pd.to_datetime(df1['Acc_end_date'])
# getting todays date and creating an array with those number of Acc_id and substracting
todays_date = []
for i in range(df1.shape[0]):
temp = date.today()
todays_date.append(temp)
np.array(todays_date)
df2 = df1[df1['Acc_end_date'] - todays_date>30]我得到的错误是:
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'list'给出的错误如下:
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'试试看:
df2 = df1[only_inactive['Acc_end_date'] - date.today()>30]我无法得到正确的日期,并以正确的格式进行减法操作。
发布于 2022-06-20 13:58:34
IIUC,您可以使用:
df2 = df1[pd.to_datetime(df1['Acc_end_date'], dayfirst=False)
.rsub(pd.Timestamp('today')).gt('30d')]产出:
Acc_id Acc_name Acc_end_date
0 qqq-1 test1 12/31/2021
4 kkk-6 test5 03/16/2022https://stackoverflow.com/questions/72688211
复制相似问题