Reputation: 19405
Consider the following very simple pandas dataset:
df = pd.DataFrame({'A': [pd.datetime(2015,02,02)]})
df
Out[53]:
A
0 2015-02-02
Here, the format of the time variable A
is a datetime64
:
df.dtypes
Out[54]:
A datetime64[ns]
dtype: object
Now, I save this dataframe as a csv, and load it using read_csv()
with the parse_dates
option.
df.to_csv('P:test.csv')
df2=pd.read_csv('P:test.csv',parse_dates='A')
df2.dtypes
Out[59]:
Unnamed: 0 int64
A object
dtype: object
The parser does not recognize the date, so I need to manually run to_datetime
again (its a loss of time)...
There should be a way to make Pandas parse correctly the date Pandas itself generated, right? What is wrong here?
Thanks!
Upvotes: 1
Views: 842
Reputation: 85622
You need to specify a list of column names:
parse_dates=['A']
Example:
>>> pd.read_csv('date.csv', parse_dates=['A']).dtypes
Unnamed: 0 int64
A datetime64[ns]
dtype: object
dtypes: datetime64[ns](1), int64(1)
memory usage: 24.0 bytes
Upvotes: 4