Pandas pct_change 给出的答案与手册略有不同
Pandas pct_change gives slightly different answers to manual
谁能解释为什么 pct_change 函数在使用更多手动计算时给出的数字略有不同:
pct_change函数:
print(prices)
0
0 1035.23
1 1032.47
print(prices.pct_change(1))
0
0 NaN
1 -0.002666
更多手动功能
(prices - prices.shift(1))/prices
0
0 NaN
1 -0.002673
这里的差异背后的原因是什么?
问题是第二个公式是错误的:
prices = pd.DataFrame({0:[1035.23,1032.47]})
print (prices)
print(prices.pct_change(1))
0
0 NaN
1 -0.002666
print(prices/(prices.shift())-1)
0
0 NaN
1 -0.002666
正如评论中 所指出的:
print((prices - prices.shift(1))/prices.shift(1))
0
0 NaN
1 -0.002666
谁能解释为什么 pct_change 函数在使用更多手动计算时给出的数字略有不同:
pct_change函数:
print(prices)
0
0 1035.23
1 1032.47
print(prices.pct_change(1))
0
0 NaN
1 -0.002666
更多手动功能
(prices - prices.shift(1))/prices
0
0 NaN
1 -0.002673
这里的差异背后的原因是什么?
问题是第二个公式是错误的:
prices = pd.DataFrame({0:[1035.23,1032.47]})
print (prices)
print(prices.pct_change(1))
0
0 NaN
1 -0.002666
print(prices/(prices.shift())-1)
0
0 NaN
1 -0.002666
正如评论中
print((prices - prices.shift(1))/prices.shift(1))
0
0 NaN
1 -0.002666