我的数据是股票价格的时间序列。包含 52 只股票的投资组合、大约 120 天的数据以及每只股票的日期时间戳。我将整洁的文件转换为数据透视表,以创建唯一的日期,并将每只股票的股票代码放在数据透视表的列中,并将收盘价放在数据透视表的行中。
target_final['date'] = pd.to_datetime(target_final['date'])
创建“target_pivot”数据框
target_pivot = target_final.pivot(index='date', columns='ticker', values='c')
target_pivot.reset_index(inplace=True) # Reset index to move 'date' from index to a regular column
target_pivot.columns.name = None # Remove the 'ticker' label from the columns
现在使用“日期”列作为时间索引创建 TimeSeries 对象
target_ts = TimeSeries.from_dataframe(
target_pivot,
time_col='date', # Use 'date' as the time column
value_cols=target_pivot.columns[1:].tolist(), # Exclude the 'date' column from value_cols
fill_missing_dates=False, # Set to False as weekends and holidays are not included
freq='B', # This is the desired frequency of the time series (daily data)
fillna_value=0, # Optional: Fill any remaining NaN with 0 (or appropriate value)
static_covariates=['static_covariate'],
hierarchy=None
)
我收到以下错误。
ValueError Traceback(最近一次调用最后一次)
在
3帧 raise_log 中的/usr/local/lib/python3.10/dist-packages/darts/logging.py(异常,记录器) 127 logger.error(异常类型+“:”+消息) 128 --> 129 引发异常 130 131
ValueError:无法使用观察到/通过的频率freq='B'
正确填写缺失的日期。并非所有输入时间戳都包含在新创建的 TimeSeries 中。有关频率别名的更多信息,请阅读 https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
这很奇怪,因为我将 fill_missing_dates 设置为 False。此外,日期是业务日期,至少是金融业务日期。所以我不确定为什么在填写缺失的日期时会出现错误。
我尝试使用多个选项更改 freq 和 fill_missing_dates 字段。
我也有同样的问题。我将 time_col 更改为 pandas 数据帧的索引,并且它有效。但随后你就失去了频率:
data_series = TimeSeries.from_dataframe(data_custom,
value_cols=['values'],
)