Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas retrieve the frequency of a time series

Tags:

python

pandas

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.

like image 445
zsljulius Avatar asked Dec 20 '13 20:12

zsljulius


2 Answers

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>
like image 168
alko Avatar answered Nov 02 '22 17:11

alko


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

import pandas as pd
pd.infer_freq(ts.index)
like image 37
sweetdream Avatar answered Nov 02 '22 19:11

sweetdream