How would I be able to subtract 1 second and 1 minute and 1 month from data['date']
column?
JavaScript
x
17
17
1
import pandas as pd
2
3
d = {'col1': [4, 5, 2, 2, 3, 5, 1, 1, 6], 'col2': [6, 2, 1, 7, 3, 5, 3, 3, 9],
4
'label':['Old','Old','Old','Old','Old','Old','Old','Old','Old'],
5
'date': ['2022-01-24 10:07:02', '2022-01-27 01:55:03', '2022-01-30 19:09:03', '2022-02-02 14:34:06',
6
'2022-02-08 12:37:03', '2022-02-10 03:07:02', '2022-02-10 14:02:03', '2022-02-11 00:32:25',
7
'2022-02-12 21:42:03']}
8
9
data = pd.DataFrame(d)
10
11
# subtract the dates by 1 second
12
date_mod_s = pd.to_datetime(data['date'])
13
# subtract the dates by 1 minute
14
date_mod_m = pd.to_datetime(data['date'])
15
# subtract the dates by 1 month
16
date_mod_M = pd.to_datetime(data['date'])
17
Advertisement
Answer
Your date
column is of type string. Convert it to pd.Timestamp
and you can use pd.DateOffset
:
JavaScript
1
2
1
pd.to_datetime(data["date"]) - pd.DateOffset(months=1, minutes=1, seconds=1)
2