Skip to content

BUG: fix for string missing data in stringdtype cast to bool and nonzero - #32418

Merged
MaanasArora merged 3 commits into
numpy:mainfrom
ngoldbaum:stringdtype-na-bug
Aug 28, 2026
Merged

MaanasArora merged 3 commits into
numpy:mainfrom
ngoldbaum:stringdtype-na-bug

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

PR summary

Both nonzero and the string to bool cast incorrectly handled string missing data. The former is missing a case for has_string_na: all the logic in the !has_string_na branch, so string missing data falls through to the size check below, which always returns False. The latter gets the boolean condition inverted: the default_string is falsey if it's empty.

Even though these are logic errors, since this is changing the nonzero tests I'm not marking this as a backport.

AI Disclosure

An AI model spotted this bug and I used an AI for code review.

@MaanasArora MaanasArora left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM. Since we're not backporting, just a few adjacent simplifications, but totally happy to merge as-is if you'd rather keep this tightly scoped.

Comment thread numpy/_core/src/multiarray/stringdtype/casts.cpp Outdated
Comment thread numpy/_core/src/multiarray/stringdtype/casts.cpp Outdated
@ngoldbaum

Copy link
Copy Markdown
Member Author

Thanks @MaanasArora! This also made me realize I can put the truthiness test into a shared helper and make this PR net-negative on LoC, even with the test 😎

@MaanasArora MaanasArora left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @ngoldbaum , that's a feat indeed :D

I traced through everything and it still looks great to me, in it goes!

stringdtype_null_is_truthy(const PyArray_StringDTypeObject *descr)
{
// nulls cannot be stored in an array without an na object
assert(descr->na_object != NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

much better guard than has_null &&, nice!

@MaanasArora
MaanasArora merged commit faaed3e into numpy:main Aug 28, 2026
98 of 99 checks passed
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants