There is no .str
accessor for datetimes and you can’t do .astype(str)
either.
Instead, use .dt.strftime
:
>>> series = pd.Series(['20010101', '20010331'])
>>> dates = pd.to_datetime(series, format="%Y%m%d")
>>> dates.dt.strftime('%Y-%m-%d')
0 2001-01-01
1 2001-03-31
dtype: object
See the docs on customizing date string formats here: strftime() and strptime() Behavior.
For old pandas versions <0.17.0
, one can instead can call .apply
with the Python standard library’s datetime.strftime
:
>>> dates.apply(lambda x: x.strftime('%Y-%m-%d'))
0 2001-01-01
1 2001-03-31
dtype: object