Set Pandas.tseries.index.datetimeindex.freq With Inferred_freq
consider the DatetimeIndex tidx tidx = pd.to_datetime(['2016-07-29', '2016-08-31', '2016-09-30']) print(tidx.freq) print(tidx.inferred_freq) print(tidx) None BM DatetimeIndex(['20
Solution 1:
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')
Solution 2:
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')
Solution 3:
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')
Post a Comment for "Set Pandas.tseries.index.datetimeindex.freq With Inferred_freq"