parse_dates cannot convert string to datetime

Viewed 41

I try to read a CSV from the link https://raw.githubusercontent.com/LinkedInLearning/data_cleaning_python_2883183/main/Ch04/challenge/traffic.csv

df = pd.read_csv('https://raw.githubusercontent.com/LinkedInLearning/data_cleaning_python_2883183/main/Ch04/challenge/traffic.csv', parse_dates=['time'])

But, the time column is still in string format.

df.dtypes 
[output]
ip        object
time      object
path      object
status     int64
size       int64
dtype: object

Interestingly, when I read a similar csv from a different url, it works. So

df = pd.read_csv('https://raw.githubusercontent.com/LinkedInLearning/data_cleaning_python_2883183/main/Ch04/solution/traffic.csv', parse_dates=['time'])

indeed converts the time column to a datetime object. Why does parse_dates fail in the first link and how can I fix it?

1 Answers

There is typo in datetimes:

1017-06-19 14:46:24

Possible solution is convert values to NaT:

df['time'] = pd.to_datetime(df['time'], errors='coerce')
Related