ValueError: buffer source array is read-only

Code Sample, a copy-pastable example if possible

>>> index = pd.date_range('2020', 'now', freq='1h') 
>>> arr = np.zeros_like(index) 
>>> arr.setflags(write=False)
>>> pd.Series(arr, index=index).resample('1d').agg('last')

~/pandas/pandas/_libs/groupby.pyx in pandas._libs.groupby.group_last()
~/pandas/pandas/_libs/ in View.MemoryView.memoryview_cwrapper()
~/pandas/pandas/_libs/ in View.MemoryView.memoryview.__cinit__()
ValueError: buffer source array is read-only

Problem description

Groupby fails on some read-only buffers (I couldn’t quickly reproduce it with .groupby() itself, sorry).

The prime solution would be to add const specifier to the input values here (and related entries):

rank_t[:, :] values,

if it were not for Cython’s non-support of const fused types (cython/cython#1772), resolved in (cython/cython#3118), but despite miniscule change only scheduled for release in Cython 3.0. I guess wait until then.

Expected Output

Resampling/groupby works with read-only arrays.

Output of pd.show_versions()

pandas 1.1.0.dev0+361.gf0b00f887
cython 0.29.14

Author: Fantashit

1 thought on “ValueError: buffer source array is read-only

  1. (Also, the “miniscule change” for this feature is only small in Cython’s master/3.0 branch. It’s not quite that small in a backport.)

Comments are closed.