Pandas get time series frequency

Is there a way to get the time series frequency 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 available.

thanks

Edit: Can we infer a frequency from time series that do not indicate a 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 change the frequency of aapl? As we know, these are business days.

+4
source share
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 rows indexed using this index

>>> ts.index.freq
<1 Hour>
+10
source

To output the frequency just use the built-in fct 'infer_freq'

import pandas as pd
pd.infer_freq(ts.index)
+8
source

All Articles