我有一个我想分解的时间序列. 数据集(火车-rame)示例(股价):

        Date    Close
7389    2014-12-24  104.589996
7390    2014-12-26  105.059998
7391    2014-12-29  105.330002
7392    2014-12-30  105.360001
7393    2014-12-31  104.5700

这是我的代码:

train_dec = copy.deepcopy(train)
train_dec.index = pd.to_datetime(train_dec['Date'])
train_dec.index.freq = 'D'

# Transf或m DataFrame into a Series
train_series = train_dec['Close']

train_decomposition = seasonal_decompose(train_series, model='additive')

train_trend = train_decomposition.trend
train_seasonal = train_decomposition.seasonal
train_residual = train_decomposition.resid

我try 了不转换为系列并使用它.try 将频率设置为"D".

我不断出现错误,例如:

Value错误:来自传递值的推断频率无与传递频率D一致

ValueErr或: You must specify a period 或 x must be a pandas object with a PeriodIndex 或 a DatetimeIndex with a freq not set to None

当我不设置频率时.

Maybe it is because the data have gaps (weekends) when there is no data point (stock price). Should I convert it to a weekly f或mat? But how can I do this if there are gaps (e.g. if I have removed outliers)?

这一定是一些微不足道的事情,但我看不到解决方案.

非常感谢您的帮助!

推荐答案

进行季节分解时需要指定时期:

import pandas as pd
import numpy as np
from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.pyplot as plt
import copy

data = {
    'Date': ['2014-12-24', '2014-12-26', '2014-12-29', '2014-12-30', '2014-12-31'],
    'Close': [104.589996, 105.059998, 105.330002, 105.360001, 104.5700]
}
train = pd.DataFrame(data)

train['Date'] = pd.to_datetime(train['Date'])
train.set_index('Date', inplace=True)

idx = pd.date_range(start=train.index.min(), end=train.index.max(), freq='D')
train = train.reindex(idx)

train['Close'] = train['Close'].ffill()

decomposition = seasonal_decompose(train['Close'], model='additive', period=3)  
fig = decomposition.plot()
plt.show()

enter image description here

Python相关问答推荐

Python中是否有方法从公共域检索搜索结果

对某些列的总数进行民意调查,但不单独列出每列

Polars:用氨纶的其他部分替换氨纶的部分

我们可以为Flask模型中的id字段主键设置默认uuid吗

递归访问嵌套字典中的元素值

不允许访问非IPM文件夹

使用Python从URL下载Excel文件

Pandas GroupBy可以分成两个盒子吗?

Python避免mypy在相互引用中从另一个类重定义类时失败

在Google Drive中获取特定文件夹内的FolderID和文件夹名称

使用__json__的 pyramid 在客户端返回意外格式

如何在FastAPI中替换Pydantic的constr,以便在BaseModel之外使用?'

使用python playwright从 Select 子菜单中 Select 值

如何从比较函数生成ngroup?

如何将相同组的值添加到嵌套的Pandas Maprame的倒数第二个索引级别

应用指定的规则构建数组

极柱内丢失类型信息""

Scipy差分进化:如何传递矩阵作为参数进行优化?

如何根据一定条件生成段id

在pandas中,如何在由两列加上一个值列组成的枢轴期间或之后可靠地设置多级列的索引顺序,