ValueError:数据框必须具有“ ds”和“ y”列,分别包含日期和值

时间:2020-07-14 16:40:52

标签: python pandas

我已经以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

i已将源数据格式化为ds和y格式。日期和值的格式正确。我不确定是什么代码或源数据丢失导致值错误出现

1 个答案:

答案 0 :(得分:0)

我有同样的问题;原来我有dsy大写。一旦在.csv文件中将它们设置为小写,就可以正常工作。