Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas pct_change() function returning wrong value

Tags:

python

pandas

I have a script that keep messing up and I am not sure what the source is. It seems to work sometimes and not others. Here is an example.

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

This is very strange and I honestly have no idea what the source of error might be. The percent change function seems to be the source of it and it works sometimes and not others seemingly at random. Has anyone else dealt with something like this before? Is there anything under the hood or unstable about my code? It also messes up elsewhere in the code, but this is the most simple of returns calculations and it still messes up. Does anyone have any idea what is going on?

like image 700
jjvandermade Avatar asked Mar 23 '26 19:03

jjvandermade


1 Answers

I confirm the problem. I was having issues with pct_change, saw this and checked my version of numexpr and found it to be 2.4.4.

In fairness to the developers of pandas and numexpr, I think I may have tried to upgrade pandas via pip instead of through conda and perhaps this caused the issues.

To fix the problem, I think did the following on windows:

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

After that things seemed to work correctly.

like image 56
oxer Avatar answered Mar 25 '26 08:03

oxer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!