Uh oh!
There was an error while loading. Please reload this page.
GH-51046: [Python][Interchange] Preserve the categorical ordered flag in from_dataframe - #51045
GH-51046: [Python][Interchange] Preserve the categorical ordered flag in from_dataframe#51045Kayvan-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: |
jorisvandenbossche
commented
Sep 3, 2026
This looks like a good fix, but note that the interchange protocol is deprecated, and so IMO it is not worth it spending much time on it to update / fix things. |
Kayvan-Zahiri
commented
Sep 3, 2026
Understood, and that is your call to make. Happy to close this if you would rather not One thing worth separating out before I do, because it is independent of the interchange desc_cat_table=col_result.describe_categoricaldesc_cat_result=col_result.describe_categoricalassertdesc_cat_table["is_ordered"] ==desc_cat_result["is_ordered"]
assertdesc_cat_table["is_dictionary"] ==desc_cat_result["is_dictionary"]Both sides are So there are two things in this PR: the Tell me which you prefer and I will do it:
Not looking to spend your time on it either way. I raised it because a test that cannot |
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.