BUG: assert_series_equal wrong with check_category_order=False and NAs (GH#62008) - #67723
Open
jbrockmendel wants to merge 1 commit into
Open
BUG: assert_series_equal wrong with check_category_order=False and NAs (GH#62008)#67723jbrockmendel wants to merge 1 commit into
jbrockmendel wants to merge 1 commit into
Conversation
…s (GH#62008) Missing values have code -1, and Index.take(-1) without allow_fill wraps around to the last category. Two Categoricals holding equal values but storing their categories in a different order therefore resolved the NA position to different labels and compared unequal. Compare the codes recoded against the sorted categories instead. Filling the taken values with NaN would work for the reported case but casts integer categories to float64, so 2**53 and 2**53 + 1 would collapse into one value and genuinely different Categoricals would compare equal. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
closes #62008
Missing values have code
-1, andIndex.take(-1)withoutallow_fillwraps around to the last category.assert_categorical_equalcompared values vialeft.categories.take(left.codes), so two Categoricals holding equal values but storing their categories in a different order resolved the NA position to different labels and compared unequal:This recodes both sides against the sorted categories and compares the codes, so
-1stays-1and can only match-1.Passing
allow_fill=True, fill_value=np.nantotake— the approach in the closed GH-62011, GH-62017 and GH-66116 — fixes the reported case but casts integer categories to float64, so2**53and2**53 + 1collapse into one value and genuinely different Categoricals start comparing equal. There is a regression test pinning that.Scope note: only
assert_series_equalis affected on the public surface.assert_frame_equalandassert_index_equaldo not accept or forwardcheck_category_order, and the converse failure (unequal objects comparing equal) is not reachable throughassert_series_equal, which compares values before it reachesassert_categorical_equal.While here:
check_category_order=Falseis still silently ignored when the categories cannot be sorted (mixed types), because theexcept TypeErrorfallback compares the categories in their original order. That is pre-existing and untouched here; the new tests pin the current behavior.AI disclosure: drafted with Claude Code (
claude opus 5 (high)), which wrote the fix and tests and ran the test suite; reviewed and revised by me.