Skip to content

Commit 5c024e2

Browse files
mroeschkefeefladder
authored andcommitted
BUG: rolling with Int64 (pandas-dev#43174)
1 parent 73418e2 commit 5c024e2

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ Groupby/resample/rolling
356356
- Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`)
357357
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
358358
- Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`)
359-
-
359+
- Bug in :meth:`Series.rolling` when the :class:`Series` ``dtype`` was ``Int64`` (:issue:`43016`)
360360

361361
Reshaping
362362
^^^^^^^^^

Diff for: pandas/core/window/rolling.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
from pandas.core.algorithms import factorize
5252
from pandas.core.apply import ResamplerWindowApply
53+
from pandas.core.arrays import ExtensionArray
5354
from pandas.core.base import (
5455
DataError,
5556
SelectionMixin,
@@ -317,7 +318,10 @@ def _prep_values(self, values: ArrayLike) -> np.ndarray:
317318
# GH #12373 : rolling functions error on float32 data
318319
# make sure the data is coerced to float64
319320
try:
320-
values = ensure_float64(values)
321+
if isinstance(values, ExtensionArray):
322+
values = values.to_numpy(np.float64, na_value=np.nan)
323+
else:
324+
values = ensure_float64(values)
321325
except (ValueError, TypeError) as err:
322326
raise TypeError(f"cannot handle this type -> {values.dtype}") from err
323327

Diff for: pandas/tests/window/test_dtypes.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
from pandas import (
5+
NA,
56
DataFrame,
67
Series,
78
)
@@ -76,6 +77,14 @@ def test_series_dtypes(method, data, expected_data, coerce_int, dtypes, min_peri
7677
tm.assert_almost_equal(result, expected)
7778

7879

80+
def test_series_nullable_int(any_signed_int_ea_dtype):
81+
# GH 43016
82+
s = Series([0, 1, NA], dtype=any_signed_int_ea_dtype)
83+
result = s.rolling(2).mean()
84+
expected = Series([np.nan, 0.5, np.nan])
85+
tm.assert_series_equal(result, expected)
86+
87+
7988
@pytest.mark.parametrize(
8089
"method, expected_data, min_periods",
8190
[

0 commit comments

Comments
 (0)