python - 为什么我无法计算一阶差分时间序列的自相关函数?

标签 python pandas statistics time-series

我有一个时间序列,其中包含 2004 年至 2017 年间“饮食”一词每月被搜索的次数。从图中可以明显看出,该时间序列具有季节性,但我想计算自相关函数来证实这一点。

可以在此处找到时间序列的数据:Google trends words 。这是数据的基本图:

import pandas as pd

df = pd.read_csv('data.csv')
df.columns = ['year_month', 'diet', 'gym', 'finance']

# convert year-month column into datetime
df.month = pd.to_datetime(df.year_month)
df.set_index('year_month', inplace=True)

df['diet'].plot()

enter image description here

我首先取一阶差分来去除趋势

df['diet_first_diff'] = df['diet'].diff()

得到下图:

enter image description here

当我使用计算自相关函数时

pd.plotting.autocorrelation_plot(df['diet_first_diff']);

我得到一个空图:

enter image description here

我用原始数据(在第一次差分之前)计算自相关函数没有问题,但我不知道为什么第一次差分数据的自相关函数无法计算。知道为什么吗?

最佳答案

明白了。该系列传进pd.plotting.autocorrelation_plot()命令不能有任何 nan值(value)观。通过 1 个周期一阶差分(导致第一个观察值缺失),此方法有效 pd.plotting.autocorrelation_plot(df['diet'].diff()[1:]) .

关于python - 为什么我无法计算一阶差分时间序列的自相关函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48761320/

相关文章:

database - RSQLite RS-DBI 驱动程序 : (error in statement: no such table: test)

python - 为什么从初始化中添加零偏移量与从减法中添加零偏移量不同?

python - 在python字典中,如果存储了多个值,如何打印键的值?

python - 创建一个列表,包括行名、列名和数据框中的值

python - Pandas dataframe 使用 groupby 对子集进行反向排序

statistics - log(1-exp(x)) 的数字精度

machine-learning - MAPE 损失函数相对于 MAE 和 RMSE 的优势

python - 如何从 Python 中的存储过程获取输出参数?

python - 来自 Python Bigquery 客户端的引用不起作用

python - 当一行包含指定子字符串 pandas 时,将所有行保留在组中