How to read csv file as dtype datetime64?
<p>Now I have csv file </p>

<pre><code> date
201605
201606
201607
201608
</code></pre>

<p>I wanna get dataframe like this</p>

<p>df</p>

<pre><code>      date
0  2016-05-01
1  2016-06-01
2  2016-07-01
3  2016-08-01
</code></pre>

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

<p>You can use parameter <code>date_parser</code> in <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel="nofollow"><code>read_csv</code></a>:</p>

<pre><code>import sys
if sys.version_info.major&lt;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
</code></pre>

python csv parsing datetime pandas