使用接近无穷大的浮点数时,熊猫滚动出现错误。我在这里展示一个例子:
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
最后一位的答案应该是1!但它是 0.5。为什么在使用大数字时滚动会发疯?具有较小浮点数的相同示例:
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