Skip to content

BUG:Wrong sum in groupby rolling due to precision issues #38752

Closed
@AkariGale

Description

@AkariGale
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import numpy as np
import math
import pandas as pd

id1 = pd.date_range('2019-10-01', '2020-01-08', freq='2H').to_frame(name='stamp').reset_index(drop=True)
id2 = pd.date_range('2019-10-01', '2020-01-08').to_frame(name='stamp').reset_index(drop=True)
id1['id'] = 1
a = 2.5
id1['value'] = (math.e + a) ** (((id1.index + 10) % 40) - 20)
id2['id'] = 2
id2['value'] = np.nan
id2.loc[id2.stamp == '2019-11-15', 'value'] = 10. ** -13

df = pd.concat([id1, id2], ignore_index=True)
df.sort_values('stamp', inplace=True)

roll_sum = df.groupby('id').rolling('93D', on='stamp')['value'].sum().reset_index()
print(roll_sum[roll_sum.id == 2].iloc[-1].value)

roll_sum_2 = df[df.id == 2].groupby('id').rolling('93D', on='stamp')['value'].sum().reset_index()
print(roll_sum_2[roll_sum_2.id == 2].iloc[-1].value)

Problem description

In the code above i show that last values in rollings are not equal:

  • if i count a sum of value using rolling on full dataframe, i obtain 0.015625000000100003
  • If i filter a dataframe and keep only id=2, i obtain 1e-13.

It seems that the aggregation in the second group depends on the first group.

If i replace a on 0, i will get 2.980242e-08 in a first rolling. And if i replace a on 1, i will get a correct answer 1e-13

Expected Output

1e-13

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 3e89b4c4b1580aa890023fc550774e63d499da25
python           : 3.7.6.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.3.13-300.fc31.x86_64
Version          : #1 SMP Mon Nov 25 17:25:25 UTC 2019
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.0
numpy            : 1.17.4
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.1.1
setuptools       : 50.3.0
Cython           : None
pytest           : 5.4.1
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.5.0
html5lib         : None
pymysql          : None
psycopg2         : 2.8.4 (dt dec pq3 ext lo64)
jinja2           : 2.10.1
IPython          : None
pandas_datareader: None
bs4              : 4.9.0
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.2.1
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : 1.3.11
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issueRegressionFunctionality that used to work in a prior pandas versionWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions