Skip to content
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

ENH: Processing of .mask() for pd.NA #56844

Open
1 of 3 tasks
kuri-menu opened this issue Jan 12, 2024 · 1 comment · May be fixed by #58730
Open
1 of 3 tasks

ENH: Processing of .mask() for pd.NA #56844

kuri-menu opened this issue Jan 12, 2024 · 1 comment · May be fixed by #58730
Assignees
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@kuri-menu
Copy link

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

df = pd.DataFrame(
    {
        'A': [0, 1, 2]
    },
)

For example, suppose you have a DataFrame like the one above.

>>> df[
...     pd.Series([-1, 1, pd.NA]) < 0
... ]
	A
0	0

This works as expected.

>>>df[
...     pd.Series([-1, 1, pd.NA]).convert_dtypes() < 0
... ]
	A
0	0

This also works as expected.

>>> df['A'].mask(
...     pd.Series([-1, 1, pd.NA]) < 0,
...     100
... )

0    100
1      1
2      2
Name: A, dtype: int64

This also works as expected.

>>> df['A'].mask(
...     pd.Series([-1, 1, pd.NA]).convert_dtypes() < 0,
...     100
... )

0    100
1      1
2    100  # !?
Name: A, dtype: int64

This behavior confuses a lot of people.

Feature Description

I think most people would expect this result.

>>> df['A'].mask(
...     pd.Series([-1, 1, pd.NA]).convert_dtypes() < 0,
...     100
... )
0    100
1      1
2      2
Name: A, dtype: int64

I think you should either set the result of the logical operation on pd.NA to False

>>> pd.Series([-1, 1, pd.NA]).convert_dtypes() < 0
0     True
1    False
2    False
dtype: boolean

or change the .mask() method to make pd.NA behave the same as False.

Alternative Solutions

Using .fillna(False) will do what you expect, but I think it would be a depressing task for many people.

>>> df['A'].mask(
...     (pd.Series([-1, 1, pd.NA]).convert_dtypes() < 0).fillna(False),
...     100
... )
0    100
1      1
2      2
Name: A, dtype: int64

Additional Context

No response

@kuri-menu kuri-menu added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 12, 2024
@WillAyd WillAyd added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate and removed Needs Triage Issue that has not been reviewed by a pandas team member Enhancement labels Jan 19, 2024
@WillAyd
Copy link
Member

WillAyd commented Jan 19, 2024

Thanks for the bug report. Yea seems like mask is not properly handing pd.NA from your OP - pull requests to fix are always welcome!

mafaldam added a commit to mafaldam/pandas that referenced this issue Apr 2, 2024
This commit addresses an issue where using pandas.NA in conjunction with the mask() method resulted in unexpected behavior. The problem arose when comparing a Series containing pandas.NA with a condition within mask(), causing inconsistencies in the output. This fix ensures that pandas.NA behaves consistently with False in logical operations within mask().
mafaldam added a commit to mafaldam/pandas that referenced this issue Apr 3, 2024
This commit addresses an issue where using pandas.NA in conjunction with the mask() method resulted in unexpected behavior. The problem arose when comparing a Series containing pandas.NA with a condition within mask(), causing inconsistencies in the output. This fix ensures that pandas.NA behaves consistently with False in logical operations within mask().
mafaldam added a commit to mafaldam/pandas that referenced this issue May 8, 2024
This commit addresses an issue where using pandas.NA in conjunction with the mask() method resulted in unexpected behavior. The problem arose when comparing a Series containing pandas.NA with a condition within mask(), causing inconsistencies in the output. This fix ensures that pandas.NA behaves consistently with False in logical operations within mask().
@mafaldam mafaldam linked a pull request May 15, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
3 participants