Skip to content

BUG / API: setitem on pandas object fails if underlying numpy array is read-only #62144

@jorisvandenbossche

Description

@jorisvandenbossche

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions