Skip to content

GH-55700: [Python] Do not convert replaced non-string columns to strings in to_pandas - #55701

Open
sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/pandas-stale-string-metadata
Open

sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/pandas-stale-string-metadata

Conversation

@sohailshk

Copy link
Copy Markdown

Rationale for this change

The pandas metadata written by Table.from_pandas can describe a column as a string column (numpy_type: "str" on pandas 3, "string" for the string dtype on pandas 2). to_pandas applied the string dtype from the metadata without looking at the Arrow type, so a column replaced with Table.set_column by an integer, floating point or boolean one came back as strings (plain str columns on pandas 3, any non-string replacement type):

table = pa.Table.from_pandas(pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]}))
table.set_column(1, "b", pa.array([1, 2, 3])).to_pandas()["b"].tolist()   # ['1', '2', '3']

What changes are included in this PR?

In _get_extension_dtypes (pandas_compat.py), the existing check that skips the string dtype for dictionary columns is generalised: the string dtype from the metadata is only used when the Arrow column is a string, large string, string view, binary, large binary, binary view or null type (the string dtype decodes binary, so a string column written with a binary schema keeps reading back as strings). Dictionary columns stay skipped.

Are these changes tested?

Yes. test_to_pandas_replaced_string_column_with_stale_metadata (int64, double, boolean, timestamp and decimal replacements, for str and string columns) and test_from_pandas_string_column_with_binary_schema are new. On main the first one fails (10 failed on pandas 3.1.1, 5 on pandas 2.3.3 where plain object strings are not affected); with the change everything passes (14 passed on 3.1.1, 12 passed and 2 skipped on 2.3.3). test_pandas.py, parquet/test_pandas.py and test_feather.py pass with the change except test_dictionary_from_pandas_specified_type, which also fails on main (pandas 3.1.1: 518 passed, 1 failed; pandas 2.3.3: 511 passed, 1 failed).

Are there any user-facing changes?

Yes: to_pandas() returns the replaced column with its own dtype instead of strings.

This PR contains a "Critical Fix". Numeric values were silently converted to strings.

Was AI used for this PR?

The implementation and tests were written with AI assistance.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…o strings in to_pandas

When the pandas metadata describes a column as a string column, to_pandas
used to build it with the pandas string dtype whatever the Arrow type of
the column is. After Table.set_column replaced such a column with an
integer, floating point, boolean, timestamp or decimal one, the values
came back as strings ('1', '2', '3'). Only apply the string dtype from
the metadata when the Arrow column is a string or binary type (the string
dtype decodes both) or null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant