0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! I had no idea. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.