3

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?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Heisenberg
  • 4,787
  • 9
  • 47
  • 76

2 Answers2

2

Maybe this answer will help you to convert your column to datetime object https://stackoverflow.com/a/26763793/5982925

Community
  • 1
  • 1
marco
  • 107
  • 1
  • 12
2

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
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252