Pandas.DataFrame.rolling does not work with huge floats

I have an error while pumping pandas when using a float close to infinity. Here I give an example:

import pandas as pd
series = pd.Series(1.,index = pd.date_range('2015-01-01', periods=6))
series[series.index[2]] = 1e19
series
2015-01-01    1.000000e+00
2015-01-02    1.000000e+00
2015-01-03    1.000000e+19
2015-01-04    1.000000e+00
2015-01-05    1.000000e+00
2015-01-06    1.000000e+00
Freq: D, dtype: float64
series.rolling('2D', closed = 'left').mean()
2015-01-01             NaN
2015-01-02    1.000000e+00
2015-01-03    1.000000e+00
2015-01-04    5.000000e+18
2015-01-05    5.000000e+18
2015-01-06    5.000000e-01
Freq: D, dtype: float64

The answer in the last bit should be 1! but it is 0.5. Why are nuts rolling when using large numbers? same example with a smaller float:

series[series.index[2]] = 1e9
series.rolling('2D', closed = 'left').mean()
2015-01-01            NaN
2015-01-02            1.0
2015-01-03            1.0
2015-01-04    500000000.5
2015-01-05    500000000.5
2015-01-06            1.0
Freq: D, dtype: float64
+6
source share
1 answer

The problem is not pandas. I tried the same in R with the rollmean function, and it gives the same result as pandas. It does not work for a value of 1e16 or higher. I believe this is due to the way the system handles float.

+1
source

All Articles