Set pandas.tseries.index.DatetimeIndex.freq with inferred_freq

前端 未结 3 1740
鱼传尺愫
鱼传尺愫 2021-01-01 02:29

consider the DatetimeIndex tidx

tidx = pd.to_datetime([\'2016-07-29\', \'2016-08-31\', \'2016-09-30\'])
print(tidx.freq)
print(tidx         


        
相关标签:
3条回答
  • 2021-01-01 02:39

    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')
    
    0 讨论(0)
  • 2021-01-01 02:40

    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')
    
    0 讨论(0)
  • 2021-01-01 02:41

    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')
    
    0 讨论(0)
提交回复
热议问题