fix(utils): make is_sorted_nb reject NaN to match is_sorted (closes #868) - #870
Open
caiyi0616 wants to merge 1 commit into
Open
fix(utils): make is_sorted_nb reject NaN to match is_sorted (closes #868)#870caiyi0616 wants to merge 1 commit into
caiyi0616 wants to merge 1 commit into
Conversation
Fixes polakowo#868 - is_sorted_nb incorrectly reported arrays with NaN values as sorted because `NaN < x` and `x < NaN` both return False in Numba, causing the loop to never return False for NaN-adjacent pairs. The fix adds explicit NaN checks via math.isnan() before the comparison, matching the non-Numba behavior where np.all(a[:-1] <= a[1:]) returns False when any adjacent pair involves NaN (since NaN <= NaN is False). Added 14 regression assertions covering: NaN in middle, NaN at start, adjacent NaN pairs, and single-NaN edge case (no adjacent pair = sorted). Note: Numba does not support np.isnan on scalars in object mode, so math.isnan() (from the math module) is used instead.
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.
Summary
is_sorted_nbreject unordered adjacent comparisons, matchingis_sortedwhen an array containsNaNPreviously,
<returned false for aNaNpair and the Numba implementationcould incorrectly report the array as sorted.
Root cause
NaN < xandx < NaNboth returnFalsein Numba, so the loop bodynever executed
return Falsefor adjacent NaN pairs, incorrectly treatingNaN-containing arrays as sorted.
Fix
Added explicit
math.isnan()checks before the<comparison, matching thenon-Numba behavior where
np.all(a[:-1] <= a[1:])returnsFalsewhen anyadjacent pair involves NaN (since
NaN <= NaNisFalse).Testing
pytest tests/test_utils.py -k is_sorted -v-> PASSEDtest_utils.pycontinue to passadjacent NaN pairs, and single-NaN edge case (no adjacent pair = sorted)
Changes
vectorbt/utils/array_.py: addedimport math; updated docstring;replaced single-line
if a[i+1] < a[i]with explicit NaN checktests/test_utils.py: added 14 NaN regression assertions