代码之家  ›  专栏  ›  技术社区  ›  Mainland

Python StatsModels:ValueError:预期频率D.得到M

  •  1
  • Mainland  · 技术社区  · 2 年前

    我正在使用 statsmodels.graphics 画一个 month_plot 来自中的时间序列数据 kaggle dataset 。我已将数据转换为绘图所需的每日频率平均数据。然而,我收到一个错误,上面写着 the expected data frequency is D, but the actual data frequency is M 其中我的实际数据已经是D。

    import pandas as pd
    from statsmodels.graphics.tsaplots import month_plot
    import matplotlib.pyplot as plt
    
    df = pd.read_csv('/kaggle/input/hourly-energy-consumption/DOM_hourly.csv')
    df.set_index('Datetime', inplace=True, drop=True)
    df.index = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
    # drop duplicated index
    df = df[~df.index.duplicated(keep='first')]
    
    # convert df to daily mean frequency dataframe
    ddf = df.resample(rule='24H', kind='interval').mean().to_period('d')
    
    # print example dataframe ddf
    #
    #                 DOM_MW
    # Datetime  
    # 2005-05-01    7812.347826
    # 2005-05-02    8608.083333
    # ...         ...
    # 2017-12-30    14079.125000
    # 2017-12-31    15872.833333
    
    # Monthly plot from the Daily frequency data
    plt.figure(figsize=(14,4))
    month_plot(ddf)
    plt.show()
    

    当前输出:正如您在上面看到的,我的 ddf 显然是一个每日频率数据。但我收到了一个奇怪的错误,说我的 ddf 数据实际上是M(每月),但预期是D(每日)。

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-7-675f2911920c> in <module>
          7 
          8 plt.figure(figsize=(14,4))
    ----> 9 month_plot(ddf)
         10 plt.show()
    
    ValueError: Expected frequency D. Got M
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Corralien    2 年前

    你尝试使用 month_plot 绘制每日数据。如果你想使用 month_plot ,您必须首先对数据进行重新采样:

    >>> help(month_plot)
    ...
        x : array_like
            Seasonal data to plot. If dates is None, x must be a pandas object
            with a PeriodIndex or DatetimeIndex with a **monthly frequency**.
    ...
    
    month_plot(ddf.resample('M').mean())
    plt.show()
    

    输出:

    enter image description here