Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.transform` where applying a timezone conversion lambda function would drop timezone information (:issue:`27496`)
- Bug in windowing over read-only arrays (:issue:`27766`)
-
-

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,11 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:
except (ValueError, TypeError):
raise TypeError("cannot handle this type -> {0}".format(values.dtype))

# Always convert inf to nan
values[np.isinf(values)] = np.NaN
# Convert inf to nan for C funcs
inf = np.isinf(values)
if inf.any():
values = values.copy() # GH-27766, Don't write into user's data
Copy link
Member

Choose a reason for hiding this comment

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

comment on previous line.

is the issue that we dont want to overwrite user's data, or that the user might pass read-only array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm, actually yeah, it's the first.

values[inf] = np.nan

return values

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,10 @@ def test_rolling_axis_count(self, axis_frame):

result = df.rolling(2, axis=axis_frame).count()
tm.assert_frame_equal(result, expected)

def test_readonly_array(self):
arr = np.array([1, 3, np.nan, 3, 5])
arr.setflags(write=False)
tm.assert_series_equal(
pd.Series(arr).rolling(2).mean(), pd.Series([np.nan, 2, np.nan, np.nan, 4])
)