diff --git a/python/pyarrow/src/arrow/python/arrow_to_pandas.cc b/python/pyarrow/src/arrow/python/arrow_to_pandas.cc index 10c4d0e16000..a0f1d5bbbed8 100644 --- a/python/pyarrow/src/arrow/python/arrow_to_pandas.cc +++ b/python/pyarrow/src/arrow/python/arrow_to_pandas.cc @@ -2523,7 +2523,8 @@ Status ConvertCategoricals(const PandasOptions& options, ChunkedArrayVector* arr } if (options.strings_to_categorical) { for (int i = 0; i < static_cast(arrays->size()); i++) { - if (is_base_binary_like((*arrays)[i]->type()->id())) { + if (is_base_binary_like((*arrays)[i]->type()->id()) || + is_binary_view_like((*arrays)[i]->type()->id())) { columns_to_encode.push_back(i); } } @@ -2557,7 +2558,8 @@ Status ConvertChunkedArrayToPandas(const PandasOptions& options, py_ref = nullptr; } - if (options.strings_to_categorical && is_base_binary_like(arr->type()->id())) { + if (options.strings_to_categorical && (is_base_binary_like(arr->type()->id()) || + is_binary_view_like(arr->type()->id()))) { if (options.zero_copy_only) { return Status::Invalid("Need to dictionary encode a column, but ", "only zero-copy conversions allowed"); diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py index 1186f87b0322..d5c936df072a 100644 --- a/python/pyarrow/tests/test_pandas.py +++ b/python/pyarrow/tests/test_pandas.py @@ -1836,10 +1836,13 @@ def test_to_pandas_categories_already_dictionary(self): result = table.to_pandas(categories=['col']) assert table.to_pandas().equals(result) - def test_table_str_to_categorical_without_na(self): + @pytest.mark.parametrize( + "string_type", [pa.string(), pa.large_string(), pa.string_view()] + ) + def test_table_str_to_categorical_without_na(self, string_type): values = ['a', 'a', 'b', 'b', 'c'] df = pd.DataFrame({'strings': values}) - field = pa.field('strings', pa.string()) + field = pa.field('strings', string_type) schema = pa.schema([field]) table = pa.Table.from_pandas(df, schema=schema) @@ -1851,10 +1854,22 @@ def test_table_str_to_categorical_without_na(self): table.to_pandas(strings_to_categorical=True, zero_copy_only=True) - def test_table_str_to_categorical_with_na(self): + # chunked array + result = table["strings"].to_pandas(strings_to_categorical=True) + expected = pd.Series(pd.Categorical(values), name="strings") + tm.assert_series_equal(result, expected) + + with pytest.raises(pa.ArrowInvalid): + table["strings"].to_pandas(strings_to_categorical=True, + zero_copy_only=True) + + @pytest.mark.parametrize( + "string_type", [pa.string(), pa.large_string(), pa.string_view()] + ) + def test_table_str_to_categorical_with_na(self, string_type): values = [None, 'a', 'b', np.nan] df = pd.DataFrame({'strings': values}) - field = pa.field('strings', pa.string()) + field = pa.field('strings', string_type) schema = pa.schema([field]) table = pa.Table.from_pandas(df, schema=schema) @@ -1866,6 +1881,15 @@ def test_table_str_to_categorical_with_na(self): table.to_pandas(strings_to_categorical=True, zero_copy_only=True) + # chunked array + result = table["strings"].to_pandas(strings_to_categorical=True) + expected = pd.Series(pd.Categorical(values), name="strings") + tm.assert_series_equal(result, expected) + + with pytest.raises(pa.ArrowInvalid): + table["strings"].to_pandas(strings_to_categorical=True, + zero_copy_only=True) + # Regression test for ARROW-2101 def test_array_of_bytes_to_strings(self): converted = pa.array(np.array([b'x'], dtype=object), pa.string())