I have some data in a csv-file:
df = pd.read_csv('chicago.csv')
df.head(5)
started_at ended_at
0 2021-01-23 16:14:19 2021-01-23 16:24:44
1 2021-01-27 18:43:08 2021-01-27 18:47:12
2 2021-01-21 22:35:54 2021-01-21 22:37:14
3 2021-01-07 13:31:13 2021-01-07 13:42:55
4 2021-01-23 02:24:02 2021-01-23 02:24:45
I need to convert the rows into datetime, because now they aren not datetime-type:
isinstance(df['started_at'], datetime.datetime)
False
I tried to do this:
df['start_date'] = pd.DataFrame({'start_date': ['started_at']})
df['start_date'] = pd.to_datetime(df['start_date'], errors='coerce', format='%Y-%m-%d %H:%M:%S')
df.head(5)
But it just gives NaT:
started_at ended_at start_date
0 2021-01-23 16:14:19 2021-01-23 16:24:44 NaT
1 2021-01-27 18:43:08 2021-01-27 18:47:12 NaT
2 2021-01-21 22:35:54 2021-01-21 22:37:14 NaT
3 2021-01-07 13:31:13 2021-01-07 13:42:55 NaT
4 2021-01-23 02:24:02 2021-01-23 02:24:45 NaT
What is wrong with my data/code?