Skip to content

API: Should isna return a copy for nullable types? #40935

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
mzeitlin11 opened this issue Apr 13, 2021 · 4 comments · Fixed by #41020
Closed

API: Should isna return a copy for nullable types? #40935

mzeitlin11 opened this issue Apr 13, 2021 · 4 comments · Fixed by #41020
Labels
API - Consistency Internal Consistency of API/Behavior Copy / view semantics NA - MaskedArrays Related to pd.NA and nullable extension arrays
Milestone

Comments

@mzeitlin11
Copy link
Member

This came up in #40651, seems worth resolving. Right now BaseMaskedArray just directly returns self._mask in the implementation of isna. This could lead to some unintuitive user-facing behavior like:

>>> ser = pd.Series([1, 1, 1], dtype="Int64")
>>> mask = ser.isna()
>>> mask[:] = True
>>> ser
0    <NA>
1    <NA>
2    <NA>
dtype: Int64

Seems like isna should return a copy instead to avoid this. One potential downside is losing an API-supported way to obtain 0-cost access to the mask (xref #34873).

@mzeitlin11 mzeitlin11 added API - Consistency Internal Consistency of API/Behavior NA - MaskedArrays Related to pd.NA and nullable extension arrays Copy / view semantics labels Apr 13, 2021
@jreback
Copy link
Contributor

jreback commented Apr 13, 2021

yeah this should return a copy

aside from that we could implement public methods that provide access with 0 copy

@jbrockmendel
Copy link
Member

could do a readonly view

@mzeitlin11
Copy link
Member Author

Readonly view seems like a good solution, only concern would be the inconsistency of

In [1]: import pandas as pd

In [2]: ser = pd.Series([1, 1])

In [3]: mask = ser.isna()

In [4]: mask[:] = True

In [5]: ser = pd.Series([1, 1], dtype="Int64")

In [6]: mask = ser.isna()

In [7]: mask[:] = True   # raises, ValueError: assignment destination is read-only

Are readonly views returned anywhere else? Copy-on-write view would be nice, but don't think that's easy w numpy.

@jorisvandenbossche
Copy link
Member

Let's leave possible read-only / copy-on-write optimizations for later. The fact that it right now doesn't return a copy is simply a bug. Modifying the resulting array shouldn't modify the original.

BTW another question here is whether isna() shouldn't rather return a nullable boolean array instead of a numpy array (I know that the result of isna() will never have missing elements by definition, but still we typically return new nullable arrays from methods on nullable arrays, except for where there is an explicit conversion to numpy).

@jorisvandenbossche jorisvandenbossche added this to the 1.3 milestone Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Copy / view semantics NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants