Repository navigation
BUG: Fix sort_values and factorize with Arrow null type (GH#54908) - #69463
Merged
rhshadrach merged 2 commits intoSep 28, 2026
Merged
Conversation
bodapatisaikrishna
force-pushed
the
fix/54908-arrow-null-sort
branch
from
September 25, 2026 15:51
ceefe89 to
87012d2
Compare
rhshadrach
requested changes
Sep 27, 2026
| tm.assert_frame_equal(result, expected) | ||
|
|
||
|
|
||
| def test_sort_values_null(): |
Member
There was a problem hiding this comment.
Can you either parametrize this or split it up into individual tests.
Contributor
Author
There was a problem hiding this comment.
Parametrized test_sort_values_null across ascending and split out the empty and Series cases into separate tests.
| ser = pd.Series([None, None], dtype="null[pyarrow]") | ||
| result = pd.Categorical(ser) | ||
| assert len(result.categories) == 0 | ||
| assert result.isna().all() |
Member
There was a problem hiding this comment.
Can you check the full result here directly and below.
Contributor
Author
There was a problem hiding this comment.
Updated to assert against the full expected Categorical and Series directly.
Member
|
Thanks @bodapatisaikrishna |
bodapatisaikrishna
added a commit
to bodapatisaikrishna/bodapatisaikrishna
that referenced
this pull request
Sep 29, 2026
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.
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.Check exactly one of the following, per the automated contributions policy:
Description
Sorting a DataFrame containing columns with PyArrow
nulltype (e.g.df.sort_values(list(df.columns))) raisedValueError: Categorical categories cannot be null.Root Cause
When sorting multiple columns,
DataFrame.sort_valuesuseslexsort_indexer, which factorizes columns viafactorize(use_na_sentinel=True)and createsCategoricalfrom the resulting codes and categories. Forpa.null(),pa.chunked_array.dictionary_encode(null_encoding="mask")produces a dictionary of null type containing a null element ([<NA>]). Becauseuse_na_sentinel=Truewas passed, returning null categories violated theCategoricalDtypeinvariant that categories cannot contain nulls. This also causedpd.Categorical(s)ands.astype("category")to fail onnull[pyarrow]Series.Fix
In
ArrowExtensionArray.factorize, handlepa.types.is_null(data.type):use_na_sentinel=Trueor array is empty: return sentinel indices (-1) and an empty chunked array withpa.null()asuniques.use_na_sentinel=False: return0indices anduniquescontaining a single null scalar[None]withpa.null().