Skip to content

GH-55677: [Python] Restore pandas extension dtype of index levels in to_pandas - #55678

Open
sohailshk wants to merge 2 commits into
apache:mainfrom
sohailshk:fix/pandas-index-extension-dtype
Open

sohailshk wants to merge 2 commits into
apache:mainfrom
sohailshk:fix/pandas-index-extension-dtype

Conversation

@sohailshk

Copy link
Copy Markdown

Rationale for this change

Table.from_pandas stores the extension dtype of each index level in the pandas metadata (for example numpy_type: "Int64"), but to_pandas only applies it to data columns. Index levels come back with the default dtype for the Arrow type, and a nullable integer index with missing values becomes a float64 index:

df = pd.DataFrame({"a": [1, 2, 3]}, index=pd.Index(pd.array([1, None, 3], dtype="Int64")))
pa.Table.from_pandas(df).to_pandas().index   # Index([1.0, nan, 3.0], dtype='float64')

What changes are included in this PR?

_extract_index_level in pandas_compat.py now converts an index level with the extension dtype recorded in the metadata when that dtype implements __from_arrow__, through the dtype's __from_arrow__, so the level is converted once. A user-supplied types_mapper still takes precedence, as it does for data columns, and dictionary (categorical) levels are left to the existing path.

Are these changes tested?

Yes. test_roundtrip_index_with_extension_dtype (single index and as a MultiIndex level) covers Int64, Float32, boolean, string, UInt8 and int64[pyarrow]; it fails on main for all six and passes with the change. test_pandas.py, parquet/, interchange/ and test_feather.py otherwise give identical results with and without the change, on pandas 3.1.1 and 2.3.3.

Are there any user-facing changes?

Yes: to_pandas() (and pd.read_parquet) returns an index with the original nullable or Arrow-backed dtype instead of the default dtype for the Arrow type.

This PR contains a "Critical Fix". A nullable integer index with missing values was converted to a float index, and integers above 2**53 lose precision in that conversion (2**53 + 1 comes back as 2**53).

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

…s in to_pandas

An index level whose pandas metadata says string but whose Arrow type was
changed afterwards (for example with Table.set_column) was converted to
strings by the restored extension dtype. Only restore the string dtype
when the Arrow column is a string or binary type or null.
@sohailshk

Copy link
Copy Markdown
Author

I pushed one more commit. Restoring the extension dtype from the metadata could also turn an index level that was replaced after from_pandas (for example a str/string level swapped for integers with set_column) into strings, where main returns the integers. The string dtype is now only restored for index levels whose Arrow type is still string-like (string, binary or null), so replaced levels keep their own type. A new test covers this (6 cases, which fail without the commit); the pandas, parquet pandas and feather tests otherwise give the same results as before.

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