Python 熊猫pct_变更对手册给出的答案略有不同
有人能解释一下为什么在使用更为手动的计算时,pct_change函数给出的数字略有不同: pct_更改功能:Python 熊猫pct_变更对手册给出的答案略有不同,python,pandas,finance,Python,Pandas,Finance,有人能解释一下为什么在使用更为手动的计算时,pct_change函数给出的数字略有不同: pct_更改功能: print(prices) 0 0 1035.23 1 1032.47
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
问题是第二个公式是错误的:
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
在这个公式中,(prices-prices.shift(1))/prices
应该除以prices.shift(1))
。在这个公式中,(prices-prices.shift(1))/prices
应该除以prices.shift(1))
。