0

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?

  • Have you looked at `df` after `df['start_date'] = pd.DataFrame({'start_date': ['started_at']})`? It does not do what you think it does. Skip this and use directly `df['start_date'] = pd.to_datetime(df['started_at'], errors='coerce', format='%Y-%m-%d %H:%M:%S')` – Mr. T Mar 06 '22 at 11:51
  • just tried this, now the column with date and time is created, but isinstance(df['start_date'], datetime.datetime) still gives False – barney stinson 2 Mar 06 '22 at 11:55
  • For an overview, you can also use `print(df.dtypes)` or `df.info()`. ` – Mr. T Mar 06 '22 at 11:55
  • yeah, now I see that the column is datetime64[ns], I guess that's what I need – barney stinson 2 Mar 06 '22 at 11:58
  • Pandas is based on numpy which uses the [datetime64 format](https://numpy.org/doc/stable/reference/arrays.datetime.html). This is not identical with base Python `datetime` but can be [converted into each other](https://stackoverflow.com/a/21916253/8881141). – Mr. T Mar 06 '22 at 11:59
  • You have another option: `df = pd.read_csv('chicago.csv', parse_dates=['start_date', 'end_date'])` – Corralien Mar 06 '22 at 13:07

0 Answers0