Skip to content

pandas.DataFrame.rolling produces incorrect value if integer and float objects are mixed #20649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mustafaburny opened this issue Apr 10, 2018 · 0 comments · Fixed by #36458
Closed
Labels
Bug Window rolling, ewma, expanding
Milestone

Comments

@mustafaburny
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd

# Generate square dataframe of ones
df = pd.DataFrame(1, index=[1,2,3,4,5], columns=['a','b','c','d','e'])

# Make last column a series of type "float" instead of type "int"
df['e'] = 1.0

# Compute rolling sum across rows
print(df.rolling(window=2, min_periods=1, axis=1).sum())

# Output. Note that the last column is incorrect.
# -----------------------------------------------
#      a    b    c    d    e
# 1  1.0  2.0  2.0  2.0  1.0
# 2  1.0  2.0  2.0  2.0  1.0
# 3  1.0  2.0  2.0  2.0  1.0
# 4  1.0  2.0  2.0  2.0  1.0
# 5  1.0  2.0  2.0  2.0  1.0

Problem description

In the above example, the last column of df was coerced into a float Series. Calculating a rolling sum of df produces an incorrect result for that last column. Incidentally, the correct result is produced by coercing the entire dataframe to a float object before computing the rolling sum.

Expected Output

# Compute same rolling sum across rows, except convert all values to type "float" first
print(df.astype('float').rolling(window=2, min_periods=1, axis=1).sum())

# Output:
# -------
#      a    b    c    d    e
# 1  1.0  2.0  2.0  2.0  2.0
# 2  1.0  2.0  2.0  2.0  2.0
# 3  1.0  2.0  2.0  2.0  2.0
# 4  1.0  2.0  2.0  2.0  2.0
# 5  1.0  2.0  2.0  2.0  2.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@WillAyd WillAyd added the Window rolling, ewma, expanding label Oct 5, 2018
@mroeschke mroeschke added the Bug label May 8, 2020
@jreback jreback added this to the 1.2 milestone Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants