Heisenberg Heisenberg - 1 month ago 12
Python Question

How to read csv file as dtype datetime64?

Now I have csv file

date
201605
201606
201607
201608


I wanna get dataframe like this

df

date
0 2016-05-01
1 2016-06-01
2 2016-07-01
3 2016-08-01


so,I would like to read csvfile as datetime64.
and add the date 1.
How can I read and transform this csvfile?

Answer

You can use parameter date_parser in read_csv:

import sys
if sys.version_info.major<3:
    from StringIO import StringIO
else:
    from io import StringIO
import pandas as pd

temp=u"""date
201605
201606
201607
201608"""

dateparser = lambda x: pd.datetime.strptime(x, '%Y%m')

#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(StringIO(temp), parse_dates=[0], date_parser=dateparser)

print (df)
        date
0 2016-05-01
1 2016-06-01
2 2016-07-01
3 2016-08-01
print (df.dtypes)
date    datetime64[ns]
dtype: object