python - Python季节性分解频率参数确定
问题描述
尽管这个问题似乎已经得到了很多解决,但我无法弄清楚为什么季节性分解在我的情况下不起作用,尽管我提供了一个带有日期时间索引的数据框作为输入。这是我的数据集的示例:
Customer order actual date Sales Volumes
0 01/01/1900 300
1 10/03/2008 3000
2 15/11/2013 10
3 23/12/2013 200
4 04/03/2014 5
5 17/03/2014 30
6 22/04/2014 1
7 26/06/2014 290
8 30/06/2014 40
代码片段如下所示:
from statsmodels.tsa.seasonal import seasonal_decompose
df_agg['Customer order actual date'] = pd.to_datetime(df_agg['Customer order actual date'])
df_agg = df_agg.set_index('Customer order actual date')
df_agg.reset_index().sort_values('Customer order actual date', ascending=True)
decomposition = seasonal_decompose(np.asarray(df_agg['Sales Volumes'] ), model = 'multiplicative')
但我系统地收到以下错误:
:您必须指定一个频率,或者 x 必须是一个带有时间序列索引且频率未设置为无的 pandas 对象
您能否解释一下为什么我应该提供频率输入,尽管我使用的是带有日期时间索引的数据框?将频率作为输入参数是否有意义,而我正在寻找季节性作为seasonal_decompose 的输出?
解决方案
seasonal_decompose 函数通过 inferred_freq 获取频率。这是链接 - https://pandas-docs.github.io/pandas-docs-travis/generated/pandas.DatetimeIndex.html
另一方面,Inferred_freq 由 infer_freq 生成,而 Infer_freq 使用序列的值而不是索引。 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.infer_freq.html
这可能是为什么即使使用时间序列索引也需要将 freq 设置为一个值的原因。
如果您想知道seasonal_decompose() 中的频率 - 这是您数据的属性。因此,如果您按月收集数据,则它具有每月频率。
seasonal_decompose() 中用于计算频率的方法是:_maybe_get_pandas_wrapper_freq()。
我对seasonal_decompose() 做了一些研究,这里的链接可能会帮助您理解函数的源代码-
季节性分解的源代码 - https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/seasonal.py
退房 - _maybe_get_pandas_wrapper_freq https://searchcode.com/codesearch/view/86129760/
希望这可以帮助!让我知道你是否发现了一些有趣的东西。
推荐阅读
- python - 在 Python 中循环掷骰子模拟
- typescript - 类实例之前的打字稿装饰器
- python - 删除具有空系列的数据框中的一行
- r - 根据报告运行时间重新分组数据
- c - 在 Brian Gladman 的 AES 实现中,aes_encrypt_key128 是如何映射到 aes_xi 的?
- r - 我可以将乳胶表放入 r 数据框中吗?
- python - Dataframe Groupby 组合多行,对浮点型列求和
- vb.net - 如何使用项目管理器在 VB.Net 中生成的 My.Settings 验证设置?
- python - 如何在 vscode 设置 matplotlib 后端
- django - 如何在 Django 中创建抽象模型?