首页 > 解决方案 > 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 格式。日期和值的格式都正确。我不确定导致出现值错误的代码或源数据丢失了什么

标签: pythonpandas

解决方案


我遇到过同样的问题; 原来我已经大写ds了。y一旦我在.csv文件中将它们设为小写,它就可以正常工作。


推荐阅读