Skip to content

Commit f73e6c0

Browse files
BUG: dataframe.rolling along rows drops float16 (#42884)
1 parent 0eebe35 commit f73e6c0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Groupby/resample/rolling
277277
- Bug in :meth:`DataFrame.groupby.rolling.var` would calculate the rolling variance only on the first group (:issue:`42442`)
278278
- Bug in :meth:`GroupBy.shift` that would return the grouping columns if ``fill_value`` was not None (:issue:`41556`)
279279
- Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`)
280+
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
280281

281282
Reshaping
282283
^^^^^^^^^

pandas/core/window/rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def _create_data(self, obj: FrameOrSeries) -> FrameOrSeries:
226226
# GH: 20649 in case of mixed dtype and axis=1 we have to convert everything
227227
# to float to calculate the complete row at once. We exclude all non-numeric
228228
# dtypes.
229-
obj = obj.select_dtypes(include=["integer", "float"], exclude=["timedelta"])
229+
obj = obj.select_dtypes(include=["number"], exclude=["timedelta"])
230230
obj = obj.astype("float64", copy=False)
231231
obj._mgr = obj._mgr.consolidate()
232232
return obj

pandas/tests/window/test_rolling.py

+35
Original file line numberDiff line numberDiff line change
@@ -1432,3 +1432,38 @@ def test_rolling_float_dtype(float_dtype):
14321432
expected = DataFrame({"A": [np.nan] * 5, "B": range(10, 20, 2)}, dtype=float_dtype)
14331433
result = df.rolling(2, axis=1).sum()
14341434
tm.assert_frame_equal(result, expected, check_dtype=False)
1435+
1436+
1437+
def test_rolling_numeric_dtypes():
1438+
# GH#41779
1439+
df = DataFrame(np.arange(40).reshape(4, 10), columns=list("abcdefghij")).astype(
1440+
{
1441+
"a": "float16",
1442+
"b": "float32",
1443+
"c": "float64",
1444+
"d": "int8",
1445+
"e": "int16",
1446+
"f": "int32",
1447+
"g": "uint8",
1448+
"h": "uint16",
1449+
"i": "uint32",
1450+
"j": "uint64",
1451+
}
1452+
)
1453+
result = df.rolling(window=2, min_periods=1, axis=1).min()
1454+
expected = DataFrame(
1455+
{
1456+
"a": range(0, 40, 10),
1457+
"b": range(0, 40, 10),
1458+
"c": range(1, 40, 10),
1459+
"d": range(2, 40, 10),
1460+
"e": range(3, 40, 10),
1461+
"f": range(4, 40, 10),
1462+
"g": range(5, 40, 10),
1463+
"h": range(6, 40, 10),
1464+
"i": range(7, 40, 10),
1465+
"j": range(8, 40, 10),
1466+
},
1467+
dtype="float64",
1468+
)
1469+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)