Description
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