Reputation: 4123
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
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
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
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:
Upvotes: 1
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