pandas pct_change对手册的回答略有不同 [英] Pandas pct_change gives slightly different answers to manual

查看:115
本文介绍了 pandas pct_change对手册的回答略有不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么使用更多手动计算时pct_change函数给出的数字略有不同:

Can anyone explain why the pct_change function gives slightly different numbers when using the more manual calculation:

pct_change函数:

pct_change function:

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 

造成这种差异的原因是什么?

What is the reason behind the difference here?

推荐答案

问题是第二个公式是错误的:

Problem is second formula is wrong:

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

Andrew L 在评论中:

print((prices - prices.shift(1))/prices.shift(1))
          0
0       NaN
1 -0.002666

这篇关于 pandas pct_change对手册的回答略有不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆