zsljulius
zsljulius

Reputation: 4123

pandas retrieve the frequency of a time series

Is there a way to retrieve the frequency of a time series in pandas?

rng = date_range('1/1/2011', periods=72, freq='H')
ts =pd.Series(np.random.randn(len(rng)), index=rng)

ts.frequency or ts.period are not methods available.

Thanks

Edit: Can we infer the frequency from time series that do not specify frequency?

import pandas.io.data as web
aapl = web.get_data_yahoo("AAPL")

<class 'pandas.tseries.index.DatetimeIndex'>
[2010-01-04 00:00:00, ..., 2013-12-19 00:00:00]
Length: 999, Freq: None, Timezone: None

Can we somehow can the aapl's frequency? As we know, it's business days.

Upvotes: 19

Views: 24590

Answers (4)

sweetdream
sweetdream

Reputation: 1419

To infer the frequency, just use the built-in function infer_freq

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

Upvotes: 30

Miguel Gonzalez
Miguel Gonzalez

Reputation: 783

If your index is datetime64 but it has no frequency associated, None is returned when using the above mentioned methods.

I propose a rudimentary methodology for just aproximate the index frequency:

Being ts a pandas.Series:

abs(np.diff(ts)).mean()

Upvotes: 2

Mehmet Burak Sayıcı
Mehmet Burak Sayıcı

Reputation: 467

@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

Upvotes: 1

alko
alko

Reputation: 48397

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>

Upvotes: 14

Related Questions