Skip to content

[BUG]: Rolling.sum() calculated wrong values when axis is one and dtypes are mixed #36458

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

Merged
merged 7 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupby.tshift` failing to raise ``ValueError`` when a frequency cannot be inferred for the index of a group (:issue:`35937`)
- Bug in :meth:`DataFrame.groupby` does not always maintain column index name for ``any``, ``all``, ``bfill``, ``ffill``, ``shift`` (:issue:`29764`)
- Bug in :meth:`DataFrameGroupBy.apply` raising error with ``np.nan`` group(s) when ``dropna=False`` (:issue:`35889`)
-
- Bug in :meth:`Rolling.sum()` returned wrong values when dtypes where mixed between float and integer and axis was equal to one (:issue:`20649`)

Reshaping
^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pandas._typing import ArrayLike, Axis, FrameOrSeries, FrameOrSeriesUnion
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.core.dtypes.cast import infer_dtype_from
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -490,6 +491,9 @@ def _apply_blockwise(
return self._apply_series(homogeneous_func)

obj = self._create_data(self._selected_obj)
if self.axis == 1:
obj = obj.astype(infer_dtype_from(obj.values)[0], copy=False)
obj._mgr = obj._mgr.consolidate()
mgr = obj._mgr

def hfunc(bvalues: ArrayLike) -> ArrayLike:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,18 @@ def test_rolling_numerical_too_large_numbers():
index=dates,
)
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
("func", "value"),
[("sum", 2.0), ("max", 1.0), ("min", 1.0), ("mean", 1.0), ("median", 1.0)],
)
def test_rolling_mixed_dtypes_axis_1(func, value):
# GH: 20649
df = pd.DataFrame(1, index=[1, 2], columns=["a", "b", "c"])
df["c"] = 1.0
result = getattr(df.rolling(window=2, min_periods=1, axis=1), func)()
expected = pd.DataFrame(
{"a": [1.0, 1.0], "b": [value, value], "c": [value, value]}, index=[1, 2]
)
tm.assert_frame_equal(result, expected)