Skip to content

Commit 4341157

Browse files
meeseeksmachinegfyoung
authored andcommitted
Backport PR pandas-dev#27767: BUG: Fix windowing over read-only arrays (pandas-dev#27782)
1 parent e95d2dd commit 4341157

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.25.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Groupby/resample/rolling
118118
^^^^^^^^^^^^^^^^^^^^^^^^
119119

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

pandas/core/window.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:
235235
"cannot handle this type -> {0}" "".format(values.dtype)
236236
)
237237

238-
# Always convert inf to nan
239-
values[np.isinf(values)] = np.NaN
238+
# Convert inf to nan for C funcs
239+
inf = np.isinf(values)
240+
if inf.any():
241+
values = np.where(inf, np.nan, values)
240242

241243
return values
242244

pandas/tests/window/test_rolling.py

+8
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,11 @@ def test_rolling_axis_count(self, axis_frame):
326326

327327
result = df.rolling(2, axis=axis_frame).count()
328328
tm.assert_frame_equal(result, expected)
329+
330+
def test_readonly_array(self):
331+
# GH-27766
332+
arr = np.array([1, 3, np.nan, 3, 5])
333+
arr.setflags(write=False)
334+
result = pd.Series(arr).rolling(2).mean()
335+
expected = pd.Series([np.nan, 2, np.nan, np.nan, 4])
336+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)