block mutation of read-only array in series
State: closed

- [x] tests added / passed
- [x] passes `git diff upstream/master | flake8 --diff`
- [x] whatsnew entry

reproduction case:

``` python
In [1]: array = np.array([1, 2, 3])

In [2]: array.flags.writeable = False

In [3]: series = pd.Series(array)

In [4]: series[0] = -1

In [5]: series
Out[5]: 
0   -1
1    2
2    3
dtype: int64

In [6]: array
Out[6]: array([-1,  2,  3])
```

In the case I was running into I was passing a `numpy.memmap` which is a subclass of `ndarray` but was over a read only memory segment so attempting the setitem was causing a segfault. The new behavior matches the numpy behavior:

``` python
In [4]: series[0] = -1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-2ad0997d5cf2> in <module>()
----> 1 series[0] = -1
   ...
ValueError: assignment destination is read-only

In [5]: array[0] = -1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-f67d1d011a13> in <module>()
----> 1 array[0] = -1

ValueError: assignment destination is read-only
```


Labels:
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves

Comments:
regarding the travis failure: do you cache the compilation accross the runs? I noticed that I needed to ensure that the `.so` and `.c` was removed because for some reason the .pyd was not treated as a dependency that required recompilation.

Here's the PR where caching was added/modified
https://github.com/pydata/pandas/pull/13526

I don't really know how it works, but it doesn't seem to check `.pxd` files which would explain your issue.

Working on this in #14363

## [Current coverage](https://codecov.io/gh/pandas-dev/pandas/pull/14359?src=pr) is 85.25% (diff: 100%)

> Merging [#14359](https://codecov.io/gh/pandas-dev/pandas/pull/14359?src=pr) into [master](https://codecov.io/gh/pandas-dev/pandas/branch/master?src=pr) will not change coverage

``` diff
@@             master     #14359   diff @@
==========================================
  Files           140        140          
  Lines         50655      50655          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
  Hits          43185      43185          
  Misses         7470       7470          
  Partials          0          0          
```

> Powered by [Codecov](https://codecov.io?src=pr). Last update [233d51d...9925327](https://codecov.io/gh/pandas-dev/pandas/compare/233d51dd099f590379bd9144c40a61f6785b2b57...9925327dd60d701ef32bb7f9efc51bda5a24178d?src=pr)

Sorry about the misunderstanding around setitem vs the inner function. I will undo the micro optimizations in python and create a safe/unsafe version of the cython function. The places where we are doing things in a loop can do a check once and delegate to the unsafe version where the spot calls can use the checked version.

rebased to fix conflict in whatsnew

thanks!

maybe make an issue on pandas2 repo about this (IIRC you made a comment already), so that this is not forgotton.

Thank you! I think this is not an issue in pandas2 because series is backed with a pandas.Array instead of an ndarray.