sohailshk opened a new issue, #55700:
URL: https://github.com/apache/arrow/issues/55700

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   With pandas 3, `Table.from_pandas` records every string column as 
`numpy_type: "str"` in the schema metadata, and `to_pandas` turns that back 
into the pandas string dtype without looking at the Arrow type of the column. 
Table methods that replace a column (`set_column`, ...) keep the schema 
metadata, so after `set_column` swaps a string column for a non-string one, 
`to_pandas` converts the new values to strings:
   
   ```python
   import pandas as pd
   import pyarrow as pa
   
   df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
   table = pa.Table.from_pandas(df)
   table = table.set_column(1, "b", pa.array([1, 2, 3]))
   print(table.schema.field("b").type)    # int64
   print(table.to_pandas()["b"].tolist())  # ['1', '2', '3']  (expected [1, 2, 
3])
   ```
   
   Any non-string type is affected, for example floating point (`[1.5, 2.0, 
3.0]` becomes `['1.5', '2', '3']`), boolean, timestamps, dates and decimals. 
`pd.read_parquet` goes through the same code, so a parquet file written from 
such a table is read back with strings as well. A typical way to hit it is 
parsing a string column into numbers with `pyarrow.compute` and putting it back 
under the same name. Passing `ignore_metadata=True` to `to_pandas` avoids it.
   
   With pandas 2 and plain object strings the metadata says `object`, and the 
same code returns `[1, 2, 3]`. With pandas 2.3.3 the `string` dtype gives the 
same wrong strings as above, and with `pd.set_option("future.infer_string", 
True)` it raises `ValueError: ArrowStringArray requires a PyArrow (chunked) 
array of large_string type`. So pandas 3 makes this reachable with plain `str` 
columns.
   
   `to_pandas` already has a defensive check in `_get_extension_dtypes` that 
ignores the string dtype from the metadata when the column is dictionary 
encoded (GH-43683 / #44195). I propose extending that check so the metadata 
dtype is only used when the Arrow column is a string, large string, string 
view, binary type (the string dtype decodes binary, so `from_pandas(df, 
schema=...)` with a binary field keeps reading back as strings) or null, 
instead of keeping the metadata in sync when columns are replaced (see #15178, 
where that was ruled out).
   
   Reproduced with pyarrow 26.0.1 with pandas 3.1.1 and 2.3.3 (macOS arm64, 
Python 3.13); the check in `_get_extension_dtypes` (`pandas_compat.py`) is 
unchanged on current main.
   
   ### Component(s)
   
   Python
   
   A tested fix is ready and the PR will follow.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to