pandas 熊猫检索时间序列的频率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20711180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:28:35  来源:igfitidea点击:

pandas retrieve the frequency of a time series

pythonpandas

提问by zsljulius

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

有没有办法在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.

ts.frequency 或 ts.period 不是可用的方法。

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.

我们可以以某种方式获得 aapl 的频率吗?众所周知,现在是工作日。

回答by sweetdream

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

要推断频率,只需使用内置 fct 'infer_freq'

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

回答by alko

For DatetimeIndex

为了 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>