I have a column called "dateCreated" and it contains a date and time, and I want to convert it to a number of days.
This is my column This is the data as a text: 2021-12-20T15:58:33.948Z
I tried this
train_data['dateCreated'] = (train_data['dateCreated'] - date.today()).dt.days
CodePudding user response:
try this:
df['day']=pd.to_datetime(df['dateCreated'],format='%Y-%m-%dT%H:%M:%S:%fZ').dt.day
CodePudding user response:
if you post the data as a code I would be able to share the result
df['dateCreated'] = pd.to_datetime(df['dateCreated'])
df['diff']=df['timestamp'].apply(lambda x: (((pd.Timestamp.today()) - x).total_seconds()/(24*3600)) )
df
Sample data used
df = pd.DataFrame({'timestamp':['2022-10-01 01:00:00',
'2022-10-02 01:00:00',
'2022-10-03 01:00:00',
'2022-10-04 01:00:00',
'2022-10-05 01:00:00',
'2022-10-01 02:00:00',
'2022-10-02 02:00:00',
'2022-10-03 02:00:00',
'2022-10-04 02:00:00',
'2022-10-05 02:00:00']
}
)
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['diff']=df['timestamp'].apply(lambda x: (((pd.Timestamp.today()) - x).total_seconds()/(24*3600)) )
df
timestamp diff
0 2022-10-01 01:00:00 18.610530
1 2022-10-02 01:00:00 17.610530
2 2022-10-03 01:00:00 16.610530
3 2022-10-04 01:00:00 15.610530
4 2022-10-05 01:00:00 14.610530
5 2022-10-01 02:00:00 18.568864
6 2022-10-02 02:00:00 17.568864
7 2022-10-03 02:00:00 16.568864
8 2022-10-04 02:00:00 15.568864
9 2022-10-05 02:00:00 14.568864