Skip to content

BUG: unary operators on IntegerArray return shared mask creating inconsistencies when assigning null and non-null values #39943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks
simonjayhawkins opened this issue Feb 20, 2021 · 3 comments
Labels
Bug Copy / view semantics NA - MaskedArrays Related to pd.NA and nullable extension arrays Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@simonjayhawkins
Copy link
Member

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

 
>>> s = pd.Series([1, 2, 3], dtype="Int64")
>>> s
0    1
1    2
2    3
dtype: Int64
>>> 
>>> s2 = -s
>>> 
>>> s2
0    -1
1    -2
2    -3
dtype: Int64
>>> 
>>> s[0] = None # assigning None
>>> 
>>> s
0    <NA>
1       2
2       3
dtype: Int64
>>> 
>>> s2
0    <NA>  # <--- expected -1
1      -2
2      -3
dtype: Int64
>>> 
>>> s[1] = 10  # assigning  a value is ok
>>> s
0    <NA>
1      10
2       3
dtype: Int64
>>> 
>>> s2
0    <NA>
1      -2
2      -3
dtype: Int64
>>> 
>>> s2[2] = None
>>> s2
0    <NA>
1      -2
2    <NA>
dtype: Int64
>>> 
>>> s
0    <NA>
1      10
2    <NA>  # expected 3
dtype: Int64
>>> 
>>> s.values._data is s2.values._data
False
>>> 
>>> s.values._mask is s2.values._mask
True

Problem description

shared mask leads to an inconsistency when assigning a non-null value compared to assigning a null value

also for __invert__ in BaseMaskedArray so will also affect other nullable arrays

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

@simonjayhawkins simonjayhawkins added Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays Copy / view semantics labels Feb 20, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 22, 2021
@simonjayhawkins
Copy link
Member Author

simonjayhawkins commented Feb 22, 2021

this was working in 1.0.5 so will label as regression

first bad commit: [67aae80] CLN: avoid values_from_object in NDFrame (#32422)

this caused a regression, #36063 and the fix, #36303 introduced this bug. cc @dsaxton

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Feb 22, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.3 milestone Feb 22, 2021
@dsaxton
Copy link
Member

dsaxton commented Feb 22, 2021

Thanks @simonjayhawkins, looks like we should be copying the mask as well. I'll try to put up a PR to fix today.

@simonjayhawkins
Copy link
Member Author

closed in #39971

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Copy / view semantics NA - MaskedArrays Related to pd.NA and nullable extension arrays Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

2 participants