pandas pct_change()函数返回错误的值

2024-09-29 02:14:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,一直搞砸,我不知道来源是什么。它有时似乎有效,有时不起作用。这里有一个例子。在

In [29]: pricesEquities=financialMarketPrices['equities'].dropna(axis[0,1],how='all'))

In [30]: pricesEquities.head()
Out[30]: 
               aud      cad     chf      eur     gbp      inr     jpy     sek  \
date                                                                            
2003-01-01  3007.1  6614.54  4630.8  2386.41  3940.4  1100.15  843.29  493.20   
2003-01-02  3027.6  6740.05  4630.8  2522.24  4009.5  1093.05  843.29  514.79   
2003-01-03  3055.5  6772.66  4899.5  2502.19  4004.9  1089.60  843.29  519.75   
2003-01-06  3075.4  6837.27  4916.2  2529.86  4001.4  1084.35  860.07  519.75   
2003-01-07  3074.5  6802.78  4887.0  2504.91  3957.4  1081.80  853.93  518.66   

               usd  
date                
2003-01-01  879.82  
2003-01-02  909.03  
2003-01-03  908.59  
2003-01-06  929.01  
2003-01-07  922.93  

In [31]: returnsEquities = pricesEquities.pct_change().iloc[1:]

In [32]: returnsEquities.head()
Out [32]:    aud     cad  chf     eur  gbp     inr  jpy     sek    usd
date                                                                 
2003-01-02   -1  414.00  NaN  189.23  NaN  145.11  NaN  210.13    NaN
2003-01-03  NaN     NaN  NaN     NaN    0     NaN  NaN  217.26    NaN
2003-01-06  NaN     NaN  NaN     NaN    0     NaN    0     NaN  65.33
2003-01-07  NaN  436.00  NaN  191.79    0  146.50    0     NaN  60.63
2003-01-08  NaN  452.67  NaN  188.93    0  147.43    0  214.14  59.13

这很奇怪,我真的不知道错误的来源是什么。百分比变化函数似乎是它的来源,它有时起作用,而不是其他看起来随机的。以前有人处理过类似的事情吗?我的代码有什么不稳定的地方吗?它也会在代码的其他地方出错,但这是最简单的返回计算,它仍然会出错。有人知道发生了什么吗?在


Tags: indate来源nanouteurheadgbp
2条回答

问题解决:我们使用的是continuum anaconda,我们认为安装错误。我们重新安装了,现在一切都正常了。在

我确认了这个问题。我在更改pct_时遇到了问题,看到这个,检查了我的numexpr版本,发现它是2.4.4。在

为了公平地对待pandas和numexpr的开发者,我认为我可能试图通过pip而不是conda来升级pandas,这可能导致了问题。在

为了解决这个问题,我认为在windows上执行了以下操作:

/path/to/Anaconda/Scripts/conda.exe update conda
/path/to/Anaconda/Scripts/conda.exe update anaconda

从那以后一切似乎都正常了。在

相关问题 更多 >