您好,我需要在30天之前获取日期,它以这种datetime格式显示。
10/15/2020 4:34:27 AM 1970-01-01
10/15/2020 5:09:32 PM 1970-01-01
10/15/2020 5:01:32 PM 1970-01-01
10/15/2020 5:05:32 PM 1970-01-01
10/15/2020 5:03:32 PM 1970-01-01
答案 0 :(得分:0)
您可以使用pd.to_timedelta
,如下所示:
In [1265]: df = pd.DataFrame({'Date': ['10/15/2020 4:34:27 AM', '10/15/2020 5:09:32 PM', '10/15/2020 5:01:32 PM', '10/15/2020 5:05:32 PM', '10/15/2020 5:03:32 PM']})
In [1266]: df
Out[1266]:
Date
0 10/15/2020 4:34:27 AM
1 10/15/2020 5:09:32 PM
2 10/15/2020 5:01:32 PM
3 10/15/2020 5:05:32 PM
4 10/15/2020 5:03:32 PM
In [1273]: df.Date = pd.to_datetime(df.Date)
In [1274]: df['subtracted_date'] = df['Date'] - pd.to_timedelta(30, unit='d')
In [1275]: df
Out[1275]:
Date subtracted_date
0 2020-10-15 04:34:27 2020-09-15 04:34:27
1 2020-10-15 17:09:32 2020-09-15 17:09:32
2 2020-10-15 17:01:32 2020-09-15 17:01:32
3 2020-10-15 17:05:32 2020-09-15 17:05:32
4 2020-10-15 17:03:32 2020-09-15 17:03:32