REGR: Fix assignment bug for unary operators
State: closed

- [x] closes https://github.com/pandas-dev/pandas/issues/39943
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#code-standards) for how to run them
- [x] whatsnew entry

If there was somehow a way to copy the mask only on write so we aren't always copying that may be better. @simonjayhawkins I put this in the `IntegerArray` tests even though we're technically checking a `Series`, let me know if you'd prefer I rewrite or move elsewhere.

Labels:
name: Bug
name: NA - MaskedArrays
description: Related to pd.NA and nullable extension arrays

Comments:
> If there was somehow a way to copy the mask only on write so we aren't always copying that may be better. 

for a backport, let's keep this simple.
> If there was somehow a way to copy the mask only on write so we aren't always copying that may be better. @simonjayhawkins I put this in the IntegerArray tests even though we're technically checking a Series, let me know if you'd prefer I rewrite or move elsewhere.

this is not necessary, these copies are *very* cheap, semantics are way more important.
lgtm. waiting on CI.
TestAccessor.test_from_coo also failing on master https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=54759&view=logs&j=4b4d380c-92b5-58cf-5b80-2c6c9b1d5805&t=8eab1181-1335-58ed-7ed5-a271d9d86005&l=80

<details>

```
2021-02-23T01:07:24.4100333Z 
2021-02-23T01:07:24.4100753Z ================================== FAILURES ===================================
2021-02-23T01:07:24.4101532Z _________________________ TestAccessor.test_from_coo __________________________
2021-02-23T01:07:24.4102065Z [gw0] win32 -- Python 3.7.9 C:\Miniconda\envs\pandas-dev\python.exe
2021-02-23T01:07:24.4102361Z 
2021-02-23T01:07:24.4103135Z self = <pandas.tests.arrays.sparse.test_array.TestAccessor object at 0x000001317E6BC888>
2021-02-23T01:07:24.4103462Z 
2021-02-23T01:07:24.4103797Z     @td.skip_if_no_scipy
2021-02-23T01:07:24.4104207Z     def test_from_coo(self):
2021-02-23T01:07:24.4104597Z         import scipy.sparse
2021-02-23T01:07:24.4104947Z     
2021-02-23T01:07:24.4105283Z         row = [0, 3, 1, 0]
2021-02-23T01:07:24.4105674Z         col = [0, 3, 1, 2]
2021-02-23T01:07:24.4106045Z         data = [4, 5, 7, 9]
2021-02-23T01:07:24.4106494Z         sp_array = scipy.sparse.coo_matrix((data, (row, col)))
2021-02-23T01:07:24.4106960Z         result = pd.Series.sparse.from_coo(sp_array)
2021-02-23T01:07:24.4107356Z     
2021-02-23T01:07:24.4107834Z         index = pd.MultiIndex.from_arrays([[0, 0, 1, 3], [0, 2, 1, 3]])
2021-02-23T01:07:24.4108389Z         expected = pd.Series([4, 9, 7, 5], index=index, dtype="Sparse[int]")
2021-02-23T01:07:24.4109198Z >       tm.assert_series_equal(result, expected)
2021-02-23T01:07:24.4109817Z E       AssertionError: Attributes of Series are different
2021-02-23T01:07:24.4110213Z E       
2021-02-23T01:07:24.4110548Z E       Attribute "dtype" are different
2021-02-23T01:07:24.4110965Z E       [left]:  Sparse[float64, nan]
2021-02-23T01:07:24.4111366Z E       [right]: Sparse[int32, 0]
2021-02-23T01:07:24.4111579Z 
2021-02-23T01:07:24.4112141Z pandas\tests\arrays\sparse\test_array.py:1193: AssertionError
2021-02-23T01:07:24.4112871Z ============================== warnings summary ===============================
```

</details>
will merge after the #39979 is merged. (otherwise may not be able to auto backport)
@meeseeksdev backport 1.2.x