Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pyarrow as pa
import pandas as pd
tbl = pa.table({'a': pa.array(['a', 'b']).dictionary_encode()})
df = tbl.to_pandas(types_mapper=pd.ArrowDtype)
tbl2 = pa.Table.from_pandas(df)
df2 = tbl2.to_pandas(types_mapper=pd.ArrowDtype)
Issue Description
It seems the arrow specific dtype from pandas is carried over to the pyarrow Table (tbl2
in the example) and when trying to convert to pandas again (last line in the example constructing df2
) it somehow cannot understand the metadata. I get this exception from it:
Traceback (most recent call last):
File "test_pandas_roundtrip.py", line 7, in <module>
df2 = tbl2.to_pandas(types_mapper=pd.ArrowDtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pyarrow/array.pxi", line 883, in pyarrow.lib._PandasConvertible.to_pandas
File "pyarrow/table.pxi", line 4251, in pyarrow.lib.Table._to_pandas
File "/tmp/arrow/lib/python3.11/site-packages/pyarrow/pandas_compat.py", line 769, in table_to_dataframe
ext_columns_dtypes = _get_extension_dtypes(
^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/arrow/lib/python3.11/site-packages/pyarrow/pandas_compat.py", line 828, in _get_extension_dtypes
pandas_dtype = _pandas_api.pandas_dtype(dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pyarrow/pandas-shim.pxi", line 147, in pyarrow.lib._PandasAPIShim.pandas_dtype
File "pyarrow/pandas-shim.pxi", line 150, in pyarrow.lib._PandasAPIShim.pandas_dtype
File "/tmp/arrow/lib/python3.11/site-packages/pandas/core/dtypes/common.py", line 1645, in pandas_dtype
npdtype = np.dtype(dtype)
^^^^^^^^^^^^^^^
File "/tmp/arrow/lib/python3.11/site-packages/numpy/core/_internal.py", line 176, in _commastring
raise ValueError(
ValueError: format number 1 of "dictionary<values=string, indices=int32, ordered=0>[pyarrow]" is not recognized
Expected Behavior
I expect to get the same DataFrame as the first DataFrame df
.
Installed Versions
Comment From: bretttully
This looks the same as https://github.com/pandas-dev/pandas/issues/53011
Comment From: wenjuno
Hi @bretttully , thanks for pointing that out, I didn't notice that ticket. Yes I think that the root cause of the problem is probably the same. I think my example is narrower in the scope where there is no file operation involved, the problem is in roundtripping the data type between pandas and pyarrow. I guess any nontrivial or pyarrow specific data type will trigger the same problem. Thanks.