Repository navigation
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
The pandas metadata written by
Table.from_pandascan describe a column as a string column (numpy_type: "str"on pandas 3,"string"for the string dtype on pandas 2).to_pandasapplied the string dtype from the metadata without looking at the Arrow type, so a column replaced withTable.set_columnby an integer, floating point or boolean one came back as strings (plainstrcolumns on pandas 3, any non-string replacement type):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, forstrandstringcolumns) andtest_from_pandas_string_column_with_binary_schemaare 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.pyandtest_feather.pypass with the change excepttest_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:
Reviewed before submission by: