I was wondering if pandas is capable of automatically detecting which columns are datetime objects and read those columns in as dates instead of strings?
I am looki
I would use pd.to_datetime
, and catch exceptions on columns that don't work. For example:
import pandas as pd
df = pd.read_csv('test.csv')
for col in df.columns:
if df[col].dtype == 'object':
try:
df[col] = pd.to_datetime(df[col])
except ValueError:
pass
df.dtypes
# (object, datetime64[ns], int64)
I believe this is as close to "automatic" as you can get for this application.