Skip to content

BUG: dataframe.rolling along rows drops float16 #42884

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 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the uint dtypes as well

"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)