pandas retrieve the frequency of a time series

@sweetdream 's answer is pretty good actually, because frequency of the data is not always kept as an attribute to the index, so this won't work if it isn't specified:

df.index.freq

@sweetdream mentioned the infer_freq solution, which leads to another day that I'm again amazed by Pandas, that infers the frequency by looking at the index. But sometimes it doesn't work, and there are another way of finding.

Both should work:

text_freq_of_hourly_data_infer_freq = pd.infer_freq(df.index)
text_freq_of_hourly_data_inferred_freq = df.index.inferred_freq

They should both return 'H', but if dataframe is not sorted, it will fail on inferring and it will return None as it is stated on documentation. So you should sort the index.

And don't forget to give "index" to it, not the dataframe, it can infer from the column instead of index if it's specified, again documentation tells, in the index.

If passed a Series will use the values of the series (NOT THE INDEX).

References:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DatetimeIndex.inferred_freq.html?highlight=infer_freq

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.infer_freq.html?highlight=infer_freq


To infer the frequency, just use the built-in fct 'infer_freq'

import pandas as pd
pd.infer_freq(ts.index)

For DatetimeIndex

>>> rng
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-01-01 00:00:00, ..., 2011-01-03 23:00:00]
Length: 72, Freq: H, Timezone: None
>>> len(rng)
72
>>> rng.freq
<1 Hour>
>>> rng.freqstr
'H'

Similary for series indexed with this index

>>> ts.index.freq
<1 Hour>

Tags:

Python

Pandas