pandas.DatetimeIndex frequency is None and can't be set pandas.DatetimeIndex frequency is None and can't be set pandas pandas

pandas.DatetimeIndex frequency is None and can't be set


You have a couple options here:

  • pd.infer_freq
  • pd.tseries.frequencies.to_offset

I suspect that errors down the road are caused by the missing freq.

You are absolutely right. Here's what I use often:

def add_freq(idx, freq=None):    """Add a frequency attribute to idx, through inference or directly.    Returns a copy.  If `freq` is None, it is inferred.    """    idx = idx.copy()    if freq is None:        if idx.freq is None:            freq = pd.infer_freq(idx)        else:            return idx    idx.freq = pd.tseries.frequencies.to_offset(freq)    if idx.freq is None:        raise AttributeError('no discernible frequency found to `idx`.  Specify'                             ' a frequency string with `freq`.')    return idx

An example:

idx=pd.to_datetime(['2003-01-02', '2003-01-03', '2003-01-06'])  # freq=Noneprint(add_freq(idx))  # inferredDatetimeIndex(['2003-01-02', '2003-01-03', '2003-01-06'], dtype='datetime64[ns]', freq='B')print(add_freq(idx, freq='D'))  # explicitDatetimeIndex(['2003-01-02', '2003-01-03', '2003-01-06'], dtype='datetime64[ns]', freq='D')

Using asfreq will actually reindex (fill) missing dates, so be careful of that if that's not what you're looking for.

The primary function for changing frequencies is the asfreq function. For a DatetimeIndex, this is basically just a thin, but convenient wrapper around reindex which generates a date_range and calls reindex.


It seems to relate to missing dates as 3kt notes. You might be able to "fix" with asfreq('D') as EdChum suggests but that gives you a continuous index with missing data values. It works fine for some some sample data I made up:

df=pd.DataFrame({ 'x':[1,2,4] },    index=pd.to_datetime(['2003-01-02', '2003-01-03', '2003-01-06']) )dfOut[756]:             x2003-01-02  12003-01-03  22003-01-06  4df.indexOut[757]: DatetimeIndex(['2003-01-02', '2003-01-03', '2003-01-06'],           dtype='datetime64[ns]', freq=None)

Note that freq=None. If you apply asfreq('D'), this changes to freq='D':

df.asfreq('D')Out[758]:               x2003-01-02  1.02003-01-03  2.02003-01-04  NaN2003-01-05  NaN2003-01-06  4.0df.asfreq('d').indexOut[759]: DatetimeIndex(['2003-01-02', '2003-01-03', '2003-01-04', '2003-01-05',               '2003-01-06'],              dtype='datetime64[ns]', freq='D')

More generally, and depending on what exactly you are trying to do, you might want to check out the following for other options like reindex & resample: Add missing dates to pandas dataframe


I'm not sure if earlier versions of python have this, but 3.6 has this simple solution:

# 'b' stands for business days# 'w' for weekly, 'd' for daily, and you get the idea...df.index.freq = 'b'