我已经工作了两个小时,没有任何运气,因此决定向社区寻求帮助。我可以通过diff()函数来对序列进行差分,也可以手动进行差分,现在我正尝试在python中将diff()函数反过来用于多变量时间序列。
此代码不会逆转原始数据帧第1行的差异。它基本上取反,并将值向上移动一行。可以帮我吗? df
#manually compute difference
cols = df.columns
diff_df = []
for col in cols:
diff_results = df[col] - df[col].shift(1)
diff_results = diff_results.fillna(0)
diff_df.append(diff_results)
diff_df_res = pd.concat(diff_df, axis=1)
#reverse difference
cols = df.columns
diff_df = []
for col in cols:
diff_results = df[col] + diff_df_res[col].shift(-1)
diff_df.append(diff_results)
diff_df_res2 = pd.concat(diff_df, axis=1)
df:
x1 x2 x3
2018-01-01 1336.0 184.0 17.0
2018-02-01 1204.0 157.0 15.0
2018-03-01 1603.0 208.0 25.0
2018-04-01 1287.0 187.0 21.0
2018-05-01 1412.0 220.0 28.0
diff_df_res:
x1 x2 x3
2018-01-01 0.0 0.0 0.0
2018-02-01 -132.0 -27.0 -2.0
2018-03-01 399.0 51.0 10.0
2018-04-01 -316.0 -21.0 -4.0
2018-05-01 125.0 33.0 7.0
diff_df_res2:
x1 x2 x3
2018-01-01 1204.0 157.0 15.0
2018-02-01 1603.0 208.0 25.0
2018-03-01 1287.0 187.0 21.0
2018-04-01 1412.0 220.0 28.0
2018-05-01 NaN NaN NaN
'''