Skip to content

BUG: rolling with Int64 #43174

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 5 commits into from
Aug 31, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/window/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from pandas import (
NA,
DataFrame,
Series,
)
Expand Down Expand Up @@ -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])
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we actually replace and use pd.NA as the output? or is this too big of a change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, but currently all rolling/expanding/ewm results always return np.float64 (which is documented as well). So that would be a big change to return the same dtype as the caller.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, do we have an issue about this? we should make this change in 1.4

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm no was speaking about supporting pd.NA specifically (i think rolling_apply is totally fine) if you can add one

Copy link
Member Author

Choose a reason for hiding this comment

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

I modified that issue to generally support same input/output dtypes (which include ExtensionDtypes that support pd.NA)

tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"method, expected_data, min_periods",
[
Expand Down