Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUG: Fix cast_pointwise_result with all-NA values #62352

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

Open
heoh wants to merge 10 commits into pandas-dev:main
base: main
Choose a base branch
Loading
from heoh:gh-62344

Conversation

Copy link
Contributor

@heoh heoh commented Sep 16, 2025

Description

Fix a bug in BaseMaskedArray._cast_pointwise_result with all-NA values results returned object dtype instead of preserving the original dtype.

Additional Notes

In the original issue report the behavior was described for BooleanDtype. While preparing the fix, I added tests not only for BooleanDtype but also for integer dtype under the same all-NA condition. I thought it would be reasonable to ensure consistency across multiple ExtensionArray types.
If this is out of scope or unnecessary for this PR, please let me know and I will modify the test for only the Boolean case, as reported in the issue.


def _cast_pointwise_result(self, values) -> ArrayLike:
if isna(values).all():
return type(self)._from_sequence(values, dtype=self.dtype)
Copy link
Member

@jbrockmendel jbrockmendel Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an option, but i think a more performant approach would be to adapt the dtype_if_all_nat keyword in maybe_convert_objects to support these cases

heoh reacted with thumbs up emoji
Copy link
Contributor Author

@heoh heoh Sep 17, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that approach is better. I'll think about how I can use it and apply it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the approach you suggested. (d5c6cfb)

At first, I wanted to generalize dtype_if_all_nat to only dtype_if_all_na. However, I discovered use-case in the sanitize_array() function that intentionally only handles NaT cases, and I felt that NA and NaT have different purposes in pandas.
So, I created a new parameter called dtype_if_all_na, which appears similar but is different, and implemented logic specifically for handling NA only.

I don't yet fully understand pandas' dtypes and codes, so my approach could be wrong. Please feel free to review and I'll fix or revert it.

Copy link
Member

@jbrockmendel jbrockmendel Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for trying out my idea, but for now let's go with your previous way of doing it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I think that's also good because the scope of change is narrow.
I will revert the last commit.

Whether to convert datetime, timedelta, period, interval types.
dtype_if_all_na : np.dtype, ExtensionDtype, or None, default None
Dtype to cast to if we have all-NA or all-None.
dtype_if_all_nat : np.dtype, ExtensionDtype, or None, default None
Copy link
Member

@jbrockmendel jbrockmendel Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this replace dtype_if_all_nat instead of adding a new arg?

Copy link
Member

@jbrockmendel jbrockmendel Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind, i now see that isnt viable.

Copy link
Contributor Author

@heoh heoh Sep 20, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It seems not easy. the details are above. #62352 (comment)

There will be pros and cons, but I think the logic of _cast_pointwise_result is that the current version is clearer. (d5c6cfb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@jbrockmendel jbrockmendel jbrockmendel left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

BUG: cast_pointwise_result with BooleanDtype and all-NA values

AltStyle によって変換されたページ (->オリジナル) /