Perhaps dates should be converted to a different frequency. In this case, I would suggest setting the index by dates.
#set an index by dates df.set_index(['time'], drop=True, inplace=True)
After that, you can more easily convert the date format format that you need most. Below, I sequentially convert to a number of date formats, eventually ending with a set of daily dates at the beginning of the month.
#Convert to daily dates df.index = pd.DatetimeIndex(data=df.index)
For brevity, I do not show that after each line above I run the following code:
print(df.index) print(df.index.dtype) print(type(df.index))
This gives me the following result:
Index(['2013-01-01', '2013-01-02', '2013-01-03'], dtype='object', name='time') object <class 'pandas.core.indexes.base.Index'> DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03'], dtype='datetime64[ns]', name='time', freq=None) datetime64[ns] <class 'pandas.core.indexes.datetimes.DatetimeIndex'> PeriodIndex(['2013-01', '2013-01', '2013-01'], dtype='period[M]', name='time', freq='M') period[M] <class 'pandas.core.indexes.period.PeriodIndex'> Index(['2013-01', '2013-01', '2013-01'], dtype='object') object <class 'pandas.core.indexes.base.Index'> DatetimeIndex(['2013-01-01', '2013-01-01', '2013-01-01'], dtype='datetime64[ns]', freq=None) datetime64[ns] <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
Ted M. Oct. 17 '17 at 21:30 2017-10-17 21:30
source share