-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Description
Consider the following example:
>>> ser = pd.Series(np.asarray(pa.array([1, 2, 3])), copy=False)
>>> ser
0 1
1 2
2 3
dtype: int64
>>> ser.iloc[0] = 10
...
File ~/scipy/repos/pandas/pandas/core/internals/blocks.py:1139, in Block.setitem(self, indexer, value)
1137 casted = casted[0, ...]
1138 try:
-> 1139 values[indexer] = casted
1140 except (TypeError, ValueError) as err:
1141 if is_list_like(casted):
ValueError: assignment destination is read-only
Here I create a pandas Series backed by a read-only numpy array somewhat manually through conversion from pyarrow and an explicit copy=False
, but you can also run into this in certain cases when converting from Arrow-like data to pandas (depending on the exact conversion method). And with CoW we are now returning read-only arrays from .values
etc, and so this might also become more common in non-Arrow related code.
From a user perspective, you have a pandas Series, and in general we allow to mutate a Series. So should this just always work? (regardless of how the Series was created, which is not necessarily always under the user control)
If this happens, we could automatically copy the array inplace in the Series to let the high-level pandas setitem operation work as expected (at the cost of an unexpected delayed copy, but this is similar as with CoW)