Reputation: 65600
I have a CSV file that looks like this:
date,important
July 2015,True
August 2015,False
But when I try to read it into pandas using read_csv with the parse_dates
flag, it isn't parsing the date column as dates:
df = pd.read_csv('test.csv', parse_dates=True)
df
date important
0 July 2015 True
1 August 2015 False
I guess this is because they aren't date objects in a recognised format, but is there any way around this?
I can use df.date = pd.to_datetime(df.date)
just fine, so I find it odd that I can't do that on import.
Upvotes: 0
Views: 740
Reputation:
By default, it parses the index as date and there is no index specified here. Either pass index_col=0
or specify the name of the column:
df = pd.read_csv('test.csv', parse_dates=['date'])
df
Out[30]:
date important
0 2015-07-01 True
1 2015-08-01 False
Or
df = pd.read_csv('test.txt', parse_dates=True, index_col=0)
df
Out[33]:
important
date
2015-07-01 True
2015-08-01 False
Upvotes: 1