diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 6763c3043b102..c49dc859d324b 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -271,6 +271,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby.rolling.var` would calculate the rolling variance only on the first group (:issue:`42442`) - Bug in :meth:`GroupBy.shift` that would return the grouping columns if ``fill_value`` was not None (:issue:`41556`) - Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`) +- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 8a253726ab0b6..01e11ff4b008d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -226,7 +226,7 @@ def _create_data(self, obj: FrameOrSeries) -> FrameOrSeries: # GH: 20649 in case of mixed dtype and axis=1 we have to convert everything # to float to calculate the complete row at once. We exclude all non-numeric # dtypes. - obj = obj.select_dtypes(include=["integer", "float"], exclude=["timedelta"]) + obj = obj.select_dtypes(include=["number"], exclude=["timedelta"]) obj = obj.astype("float64", copy=False) obj._mgr = obj._mgr.consolidate() return obj diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index f446eb494d5fa..aedaadb46abc6 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1432,3 +1432,38 @@ def test_rolling_float_dtype(float_dtype): expected = DataFrame({"A": [np.nan] * 5, "B": range(10, 20, 2)}, dtype=float_dtype) result = df.rolling(2, axis=1).sum() tm.assert_frame_equal(result, expected, check_dtype=False) + + +def test_rolling_numeric_dtypes(): + # GH#41779 + df = DataFrame(np.arange(40).reshape(4, 10), columns=list("abcdefghij")).astype( + { + "a": "float16", + "b": "float32", + "c": "float64", + "d": "int8", + "e": "int16", + "f": "int32", + "g": "uint8", + "h": "uint16", + "i": "uint32", + "j": "uint64", + } + ) + result = df.rolling(window=2, min_periods=1, axis=1).min() + expected = DataFrame( + { + "a": range(0, 40, 10), + "b": range(0, 40, 10), + "c": range(1, 40, 10), + "d": range(2, 40, 10), + "e": range(3, 40, 10), + "f": range(4, 40, 10), + "g": range(5, 40, 10), + "h": range(6, 40, 10), + "i": range(7, 40, 10), + "j": range(8, 40, 10), + }, + dtype="float64", + ) + tm.assert_frame_equal(result, expected)