From f2b2f0c3c356e3c77932aa22d4350d65b5da7f0a Mon Sep 17 00:00:00 2001 From: Kayvan Zahiri Date: Sat, 29 Aug 2026 21:10:51 -0700 Subject: [PATCH] [Python][Interchange protocol] Keep is_ordered when converting a categorical 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) Claude-Session: https://claude.ai/code/session_01BCtY1NAjuJ1jRvq5P6vSED --- python/pyarrow/interchange/from_dataframe.py | 3 ++- python/pyarrow/tests/interchange/test_conversion.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/pyarrow/interchange/from_dataframe.py b/python/pyarrow/interchange/from_dataframe.py index fc6530a83316..d1e9448897c7 100644 --- a/python/pyarrow/interchange/from_dataframe.py +++ b/python/pyarrow/interchange/from_dataframe.py @@ -276,7 +276,8 @@ def categorical_column_to_dictionary( col.offset) # Constructing a pa.DictionaryArray - dict_array = pa.DictionaryArray.from_arrays(indices, dictionary) + dict_array = pa.DictionaryArray.from_arrays(indices, dictionary, + ordered=categorical["is_ordered"]) return dict_array diff --git a/python/pyarrow/tests/interchange/test_conversion.py b/python/pyarrow/tests/interchange/test_conversion.py index cc105178ad87..e6053d21bff7 100644 --- a/python/pyarrow/tests/interchange/test_conversion.py +++ b/python/pyarrow/tests/interchange/test_conversion.py @@ -258,7 +258,7 @@ def test_pandas_roundtrip_categorical(): assert col_result.size() == col_table.size() assert col_result.offset == col_table.offset - desc_cat_table = col_result.describe_categorical + desc_cat_table = col_table.describe_categorical desc_cat_result = col_result.describe_categorical assert desc_cat_table["is_ordered"] == desc_cat_result["is_ordered"] @@ -380,10 +380,12 @@ def test_pyarrow_roundtrip(uint, int, float, np_float_str, @pytest.mark.parametrize("offset, length", [(0, 10), (0, 2), (7, 3), (2, 1)]) -def test_pyarrow_roundtrip_categorical(offset, length): +@pytest.mark.parametrize("ordered", [False, True]) +def test_pyarrow_roundtrip_categorical(offset, length, ordered): arr = ["Mon", "Tue", "Mon", "Wed", "Mon", "Thu", "Fri", None, "Sun"] + dict_type = pa.dictionary(pa.int32(), pa.string(), ordered=ordered) table = pa.table( - {"weekday": pa.array(arr).dictionary_encode()} + {"weekday": pa.array(arr).dictionary_encode().cast(dict_type)} ) table = table.slice(offset, length) result = _from_dataframe(table.__dataframe__())