GH-51046: [Python][Interchange] Preserve the categorical ordered flag in from_dataframe - #51045
Draft
Kayvan-Zahiri wants to merge 1 commit into
Draft
GH-51046: [Python][Interchange] Preserve the categorical ordered flag in from_dataframe#51045Kayvan-Zahiri wants to merge 1 commit into
Kayvan-Zahiri wants to merge 1 commit into
Conversation
…gorical column categorical_column_to_dictionary dropped the is_ordered flag the producer reports, so an ordered categorical column came back unordered. The pandas roundtrip test compared the result column's describe_categorical against itself, so its is_ordered assertion never ran. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCtY1NAjuJ1jRvq5P6vSED
|
Thanks for opening a pull request! This pull request has been automatically converted to a draft because its title doesn't match Arrow's required format. If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project. Then could you also rename the pull request title in the following format? or After updating the title, you can mark the pull request as ready for review. See also: |
|
|
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.
Rationale for this change
from_dataframebuilds the dictionary array without theis_orderedflag the producer reports, so an ordered categorical column comes back unordered. A pyarrow table withdictionary(int32, string, ordered=True)does not survive its own round trip, and an ordered pandasCategoricalloses its ordering on the way in. The pandas consumer passes the flag through (pd.Categorical(values, categories=categories, ordered=categorical["is_ordered"])), and our own producer reports it, so pyarrow is the only side dropping it.What changes are included in this PR?
categorical_column_to_dictionarypassesordered=categorical["is_ordered"]toDictionaryArray.from_arrays. Indices, dictionary and null handling are untouched.Are these changes tested?
Yes.
test_pyarrow_roundtrip_categoricalis now parametrized over ordered True and False, and the ordered cases fail without the change:assert table.equals(result)seesordered=1going in andordered=0coming out. I also fixed a copy-paste intest_pandas_roundtrip_categorical, which readdescribe_categoricalfrom the result column twice, so itsis_orderedassertion compared the result with itself and could never fail.Are there any user-facing changes?
An ordered dictionary column stays ordered through
from_dataframe. No API change.I used an AI assistant while finding and writing this. I checked the behavior and the tests myself before opening the PR.