diff --git a/python/pyarrow/pandas_compat.py b/python/pyarrow/pandas_compat.py index 4ff587b702b..454b630f1ac 100644 --- a/python/pyarrow/pandas_compat.py +++ b/python/pyarrow/pandas_compat.py @@ -915,15 +915,24 @@ def _get_extension_dtypes(table, columns_metadata, types_mapper, options, catego # when the metadata indicate to use the string dtype, # ignore this in case: # - it is specified to convert strings / this column to categorical - # - the column itself is dictionary encoded and would otherwise be - # converted to categorical + # - the column is not a string column, e.g. dictionary encoded or + # replaced by a column of another type after the metadata was + # written (the dtype's __from_arrow__ also accepts binary) if strings_to_categorical or name in categories: continue try: - if pa.types.is_dictionary(table.schema.field(name).type): - continue + field_type = table.schema.field(name).type except KeyError: pass + else: + if not (pa.types.is_string(field_type) + or pa.types.is_large_string(field_type) + or pa.types.is_string_view(field_type) + or pa.types.is_binary(field_type) + or pa.types.is_large_binary(field_type) + or pa.types.is_binary_view(field_type) + or pa.types.is_null(field_type)): + continue if hasattr(pandas_dtype, "__from_arrow__"): ext_columns[name] = pandas_dtype diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py index 1f09a9247e6..e92077b3ed0 100644 --- a/python/pyarrow/tests/test_pandas.py +++ b/python/pyarrow/tests/test_pandas.py @@ -4690,6 +4690,41 @@ def test_array_to_pandas_string_dtype(string_type, data): ordered=False) +@pytest.mark.parametrize("dtype", [None, "string"]) +@pytest.mark.parametrize( + "new_column", + [ + pa.array([1, 2, 3]), + pa.array([1.5, 2.5, 3.5]), + pa.array([True, False, None]), + pa.array([datetime(2020, 1, 1)] * 3), + pa.array([decimal.Decimal("1.5")] * 3), + ], +) +def test_to_pandas_replaced_string_column_with_stale_metadata(new_column, + dtype): + # The pandas metadata still describes the replaced column as a string + # column; its values must not be converted to strings. + df = pd.DataFrame({"a": [1, 2, 3], "b": pd.Series(["x", "y", "z"], dtype=dtype)}) + table = pa.Table.from_pandas(df).set_column(1, "b", new_column) + result = table.to_pandas() + tm.assert_series_equal(result["b"], new_column.to_pandas(), check_names=False) + + +@pytest.mark.parametrize("dtype", [ + pytest.param(None, marks=pytest.mark.skipif( + not _pandas_api.uses_string_dtype(), reason="requires pandas 3 str dtype")), + "string", +]) +@pytest.mark.parametrize("binary_type", [pa.binary(), pa.large_binary()]) +def test_from_pandas_string_column_with_binary_schema(dtype, binary_type): + # A string column stored as binary is still read back as strings + df = pd.DataFrame({"b": pd.Series(["x", "y", "z"], dtype=dtype)}) + schema = pa.schema([("b", binary_type)]) + result = pa.Table.from_pandas(df, schema=schema).to_pandas() + assert result["b"].tolist() == ["x", "y", "z"] + + # ---------------------------------------------------------------------- # Legacy metadata compatibility tests