Skip to content

Commit f669f94

Browse files
kerncjreback
authored andcommitted
BUG: Fix windowing over read-only arrays (#27767)
1 parent 61819ab commit f669f94

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
@@ -246,8 +246,10 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:
246246
except (ValueError, TypeError):
247247
raise TypeError("cannot handle this type -> {0}".format(values.dtype))
248248

249-
# Always convert inf to nan
250-
values[np.isinf(values)] = np.NaN
249+
# Convert inf to nan for C funcs
250+
inf = np.isinf(values)
251+
if inf.any():
252+
values = np.where(inf, np.nan, values)
251253

252254
return values
253255

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)