Repository navigation
GH-55677: [Python] Restore the extension dtype of index levels in Table.to_pandas - #55702
Open
SAMARTHJAISWAL wants to merge 1 commit into
Open
SAMARTHJAISWAL wants to merge 1 commit into
SAMARTHJAISWAL wants to merge 1 commit into
Conversation
…in Table.to_pandas
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
Table.from_pandasrecords the extension dtype of an index level in the pandas metadata (numpy_type: "Int64"), andto_pandasuses that for data columns but not for index levels. An index level is converted withChunkedArray.to_pandas, which does not look at the metadata, so it comes back with the default dtype for its Arrow type. A nullable integer index with missing values becomes a float index, and integers above 2**53 lose precision.What changes are included in this PR?
_extract_index_levelnow looks up the dtype stored for the level and, if it is a pandas extension dtype with__from_arrow__, builds the level with it. The rules follow_get_extension_dtypesfor data columns:types_mapperand an Arrow extension type take priority over the metadata.Two cases keep the conversion without metadata:
StringDtypebut the column is not a string column. This avoids giving the index the problem reported for data columns in [Python] Table.to_pandas() converts a replaced non-string column to strings with pandas 3 when the pandas metadata describes it as a string column #55700.__from_arrow__raises for the column, for example afterset_columnreplaced anInt64index column with strings. Index reconstruction is already lenient about metadata that no longer matches the table, so this falls back instead of raising. I can make it raise, as it does for data columns, if that is preferred.Are these changes tested?
Yes. New tests in
test_pandas.pyround-trip an index ofInt64(also with a value above 2**53),UInt8,Float64,boolean,stringandint64[pyarrow], a MultiIndex withInt64andbooleanlevels, and a table whose index column was replaced by one of another type.I ran the new tests and
pyarrow.tests.test_pandaswith the change applied to pyarrow 26.0.1, on pandas 3.1.1 and pandas 2.3.3: 9 of the 11 new tests fail before the change and all pass after it, and no existing test changes result. I did not build Arrow from source.Are there any user-facing changes?
Yes. An index level whose pandas extension dtype is stored in the metadata now comes back with that dtype from
Table.to_pandas(and so frompd.read_parquet) instead of the default dtype for its Arrow type. For example anInt64index with missing values is no longer returned asfloat64.ignore_metadata=Truegives the previous result.Was AI used for this PR?
PR code and description written by:
Reviewed before submission by:
The code, tests and description were written with Claude Code. I read the change and ran the tests described above.