Set pandas.tseries.index.DatetimeIndex.freq with inferred_freq
It's unclear why the docs state you can set the freq
attribute but then it doesn't persist but if you reconstruct the datetimeindex
again but pass a freq
param then it works:
In [56]:
tidx = pd.DatetimeIndex(tidx.values, freq = tidx.inferred_freq)
tidx
Out[56]:
DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'], dtype='datetime64[ns]', freq='BM')
https://stackoverflow.com/a/40223868/2336654
I asked another question to help with this. @root identified a function for converting frequency strings. So this should work
tidx.freq = pd.tseries.frequencies.to_offset(tidx.inferred_freq)
DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'],
dtype='datetime64[ns]', freq='BM')
You can directly use the DatetimeIndex
constructor with your list of strings and pass 'infer'
as the freq
:
In [2]: tidx = pd.DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'], freq='infer')
In [3]: tidx
Out[3]: DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'], dtype='datetime64[ns]', freq='BM')