首页 > 解决方案 > 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 的输出?

标签: pythonstatsmodels

解决方案


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/

希望这可以帮助!让我知道你是否发现了一些有趣的东西。


推荐阅读