python - ValueError:数据框必须有列“ds”和“y”分别包含日期和值
问题描述
我已经使用 ds 和 y 格式创建了我的数据源,但仍然收到上述错误...请参阅下面的代码
import pandas as pd
import numpy as np
import pystan
from fbprophet import Prophet
import matplotlib.pyplot as plt
plt.style.use("fivethirtyeight")
df = pd.read_csv(r'C:\Users\sussmanbl\Desktop\fb prophet ar historical2.csv')
df['ds'] = pd.to_datetime(df['ds'])
print(df.head())
df.dtypes
plt.figure(figsize=(12,8))
plt.plot(df.set_index('ds'))
plt.legend(['AR'])
m1 = Prophet(weekly_seasonality=True)
m1 = Prophet(daily_seasonality=True)
m1.fit(df)
future = m1.make_future_dataframe(periods=90)
future.tail().T
forecast = m1.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
fig1 = m1.plot(forecast)
fig2 = m1.plot_components(forecast)
runfile('C:/Users/sussmanbl/Desktop/Modelling/FB Prophet AR Historical.py',
wdir='C:/Users/sussmanbl/Desktop/Modelling')
Reloaded modules: stanfit4anon_model_ad32c37d592cdbc572cbc332ac6d2ee2_4431954053790800620
ds y
0 2017-01-03 10
1 2017-01-04 39
2 2017-01-05 19
3 2017-01-06 12
4 2017-01-09 11
错误:
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/sussmanbl/Desktop/Modelling/FB Prophet AR Historical.py',
wdir='C:/Users/sussmanbl/Desktop/Modelling')
File "C:\Users\sussmanbl.conda\envs\stan_env\lib\site-
packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\sussmanbl.conda\envs\stan_env\lib\site-
packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/sussmanbl/Desktop/Modelling/FB Prophet AR Historical.py", line 17, in
m1.fit(df)
File "C:\Users\sussmanbl.conda\envs\stan_env\lib\site-packages\fbprophet\forecaster.py", line
1082, in fit
'Dataframe must have columns "ds" and "y" with the dates and '
ValueError: Dataframe must have columns "ds" and "y" with the dates and values respectively.
df.dtypes
Out[46]:
ds datetime64[ns]
y int64
dtype: object
我已将源数据格式化为 ds 和 y 格式。日期和值的格式都正确。我不确定导致出现值错误的代码或源数据丢失了什么
解决方案
我遇到过同样的问题; 原来我已经大写ds
了。y
一旦我在.csv
文件中将它们设为小写,它就可以正常工作。
推荐阅读
- javascript - 正则表达式逗号作为可选字符
- cassandra - Apache Cassandra 中临时数据的概念
- javascript - 根据另一个数组的顺序对数组进行排序 - 对输入
- c# - 如何制作编辑表格?
- c++ - 快速选择算法返回第 K 个最小元素。递归参数问题
- azure-data-factory - 如何在 Azure 数据工厂中参数化数据集定义文件名
- python - 如何从 CSV 的 HTML 页面中删除换行符 (\n)?
- python - ChromeDriver Chrome 版本错误和安装问题
- sql - 为什么我必须添加括号才能使工会工作
- google-cloud-platform - Google Cloud Function 限制开发者/编辑者对函数的访问