Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions python/pyarrow/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down