尽管这个问题似乎已经解决了很多,但我无法弄清楚为什么季节性分解在我的情况下不起作用,尽管我提供了带有日期时间索引的数据框作为输入。这是我的数据集的一个示例:

    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')

但是我系统地得到以下错误:



您能解释一下为什么我使用带有Datetime Index的数据帧时还是要输入频率吗?将频率作为输入参数是否有意义,而我正在寻找季节性作为Seasonal_decompose的输出?

最佳答案

season_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设置为一个值的原因。

如果您想知道Season_decompose()中的频率,这是数据的属性。因此,如果您按月收集数据,则它具有每月一次的频率。

Season_decompose()中用于计算频率的方法是:_maybe_get_pandas_wrapper_freq()。

我对Seasonal_decompose()进行了一些研究,这些链接可能有助于您理解函数的源代码,

季节性分解的源代码-
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/seasonal.py

checkout -_maybe_get_pandas_wrapper_freq
https://searchcode.com/codesearch/view/86129760/

希望这可以帮助!
让我知道您是否还能找到一些有趣的东西。

10-06 05:36
查看更多