GeezerVison is showing github.com — BUG: Fix order of real and imaginary NaNs when sorting complex in descending order by MaanasArora · Pull Request #31557 · numpy/numpy
Conversation
MaanasArora
commented
Jun 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes a bug from #31345 where complex numbers with only imaginary nans get sorted before those with only real nans in descending sorts, when keeping in with our NaN semantics they should be sorted after (real nans are the primary key). This should probably be backported to 2.5, cc @seberg @charris
The longstanding less implementation is a bit confusing, but I didn't refactor/simplify and kept this change minimal. It would be good to confirm this is the correct behavior, though, as the greater is a bit hard to read (hopefully less so now):
- if greater, only push to right if the imaginary part is strictly NaN but the other is not,
- if less, only push to right if the other imaginary part is strictly NaN but this one is not,
- If real parts are equal or both are NaN, break the tie by float-like comparison of imaginary,
- Failing all (i.e., only one of the real parts is NaN), push to the right only if first is NaN.
So for the real part at least:
- If both reals are non-NaN -> branch 1-2
- If exactly one real is NaN -> branch 4
- If both reals are NaN -> branch 3
Sorry, verbose, but hopefully that clears things, thank you for reviewing!
AI Disclosure
No AI tools used.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, can simplify it more and align with the first, but I'll just apply that. Not sure what kind of merge mess caused this, but good to have the test fixed.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
charris
added a commit
that referenced
this pull request
Jun 5, 2026
BUG: Fix order of real and imaginary NaNs when sorting complex in descending order (#31557)
Reviewers
seberg
seberg approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|