diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 7b1f44d25d5ae..f197683349506 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -356,7 +356,7 @@ Groupby/resample/rolling - 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`) - Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`) -- +- Bug in :meth:`Series.rolling` when the :class:`Series` ``dtype`` was ``Int64`` (:issue:`43016`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index ab23b84a3b8c6..66ffc2600e88e 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -50,6 +50,7 @@ from pandas.core.algorithms import factorize from pandas.core.apply import ResamplerWindowApply +from pandas.core.arrays import ExtensionArray from pandas.core.base import ( DataError, SelectionMixin, @@ -317,7 +318,10 @@ def _prep_values(self, values: ArrayLike) -> np.ndarray: # GH #12373 : rolling functions error on float32 data # make sure the data is coerced to float64 try: - values = ensure_float64(values) + if isinstance(values, ExtensionArray): + values = values.to_numpy(np.float64, na_value=np.nan) + else: + values = ensure_float64(values) except (ValueError, TypeError) as err: raise TypeError(f"cannot handle this type -> {values.dtype}") from err diff --git a/pandas/tests/window/test_dtypes.py b/pandas/tests/window/test_dtypes.py index 7cd2bf4f1ca19..12c653ca3bf98 100644 --- a/pandas/tests/window/test_dtypes.py +++ b/pandas/tests/window/test_dtypes.py @@ -2,6 +2,7 @@ import pytest from pandas import ( + NA, DataFrame, Series, ) @@ -76,6 +77,14 @@ def test_series_dtypes(method, data, expected_data, coerce_int, dtypes, min_peri tm.assert_almost_equal(result, expected) +def test_series_nullable_int(any_signed_int_ea_dtype): + # GH 43016 + s = Series([0, 1, NA], dtype=any_signed_int_ea_dtype) + result = s.rolling(2).mean() + expected = Series([np.nan, 0.5, np.nan]) + tm.assert_series_equal(result, expected) + + @pytest.mark.parametrize( "method, expected_data, min_periods", [