BUG/ENH: fix casting and setitem nan behavior between StringDType and float16/32#31825
Open
ngoldbaum wants to merge 2 commits into
Open
BUG/ENH: fix casting and setitem nan behavior between StringDType and float16/32#31825ngoldbaum wants to merge 2 commits into
ngoldbaum wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
This was originally part of #31682 but @seberg asked me to split it off. I also took his comments on that version about how I handled complex values into account.
Currently, we only support correctly parsing NaN values for float64 for casting and setitem. This leads to this (IMO) strange asymmetry:
IMO this is just a straight-up bug in the implementation, which relies on coercing the value to a Python scalar, and fails to catch e.g.
np.float32('nan')because float32 scalars aren't subtypes of PyFloat. That said, it is an observable behavior change.We don't parse any complex-valued NaN as a NaN missing data value, leading to behavior like this:
This behavior is a bit more arguable to change, but under the principle that missing values should round-trip correctly via casting and that setitem should behave identically to casting, I also added behavior for complex values with
nanreal values to coerce to a null string if the na_object is NaN-like. This followes this existing behavior in NumPy:Note that the
ComplexWarninghappens unconditionally here for all complex values, even if the real part is zero.I refactored StringDType internals to reflect this behavior. This refactoring included correcting some restructuring to reflect that the float to string casts might now produce null strings where has_nan_na was ignored before.
Added tests for this new behavior for floats and complex dtypes. Also adds a test that round-trips from float64 to float64 via complex, float64, and stringdtype, including all the permutations of that should round-trip.
AI Disclosure
An AI model initially noticed this issue. I also used an AI to brainstorm and review the code changes and tests.