pandas
pandas copied to clipboard
BUG: Failures with ArrowDtype(pa.string_view())
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 pandas as pd # pandas 2.2.3 => latest
import pyarrow as pa # pyarrow 17.0.0 => latest
tab = pa.table({"names": pa.array(["a", "b", "c"], type=pa.string_view())})
# this succeeds
tab.to_pandas()
# this throws an exception
tab.to_pandas(types_mapper=pd.ArrowDtype)
Issue Description
Our library is producing Arrow binary view arrays (strings and binary) and we want to allow users to convert into Pandas DataFrame.
We are using the pd.ArrowDtype constructor to allow creating Pandas arrays that are backed with Arrow storage. The example I've attached fails also when you change "c" to None (i.e. problem hits for both nullable and non-nullable types).
Full repro with error message:
>>> import pandas as pd # pandas 2.2.3 => latest
>>> import pyarrow as pa # pyarrow 17.0.0 => latest
>>>
>>> tab = pa.table({"names": pa.array(["a", "b", "c"], type=pa.string_view())})
>>>
>>> # this succeeds
>>> tab.to_pandas()
names
0 a
1 b
2 c
>>>
>>> # this throws an exception
>>> tab.to_pandas(types_mapper=pd.ArrowDtype)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/core/frame.py", line 1214, in __repr__
return self.to_string(**repr_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/util/_decorators.py", line 333, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/core/frame.py", line 1394, in to_string
return fmt.DataFrameRenderer(formatter).to_string(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/format.py", line 962, in to_string
string = string_formatter.to_string()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/string.py", line 29, in to_string
text = self._get_string_representation()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/string.py", line 44, in _get_string_representation
strcols = self._get_strcols()
^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/string.py", line 35, in _get_strcols
strcols = self.fmt.get_strcols()
^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/format.py", line 476, in get_strcols
strcols = self._get_strcols_without_index()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/format.py", line 729, in _get_strcols_without_index
str_columns = self._get_formatted_column_labels(self.tr_frame)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/io/formats/format.py", line 809, in _get_formatted_column_labels
need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/core/dtypes/common.py", line 1119, in is_numeric_dtype
return _is_dtype_type(
^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/core/dtypes/common.py", line 1468, in _is_dtype_type
tipo = pandas_dtype(arr_or_dtype).type
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Volumes/Code/vortex/.venv/lib/python3.11/site-packages/pandas/core/dtypes/dtypes.py", line 2169, in type
raise NotImplementedError(pa_type)
NotImplementedError: string_view
>>>
This seems like a distinct issue from #59883.
Expected Behavior
I'd expect the throwing example to not throw.
Installed Versions
Device: macOS 14.4.1 M2 Max MBP
>>> pd.show_versions()
INSTALLED VERSIONS
------------------
commit : 0691c5cf90477d3503834d983f69350f250a6ff7
python : 3.11.9
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : 8.0.2
IPython : 8.26.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.3
lxml.etree : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : 8.1.1
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.7.0
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None
The failure is occurring in the type property accessor on ArrowDtype. I'm not familiar enough with Pandas internals to be sure, but my suspicion is that this if statement should include a check for is_string_view
https://github.com/pandas-dev/pandas/blob/2a10e04a099d5f1633abcdfbb2dd9fdf09142f8d/pandas/core/dtypes/dtypes.py#L2216-L2217
I don't have permissions to add but this should include the Arrow label
The proposed fix resolves the OP and doesn't break any tests for me locally. I'm not finding anything on string_view in the code or docs, do we support string views @WillAyd / @jorisvandenbossche?
I am not sure if the ArrowDtype has ever fully been scoped out, but as far as I am aware we should allow any Arrow data type to be stored within that container
ArrowDtype is AFAIK indeed quite agnostic and supporting any pyarrow data type to put into it. But then further operations on it rely on pyarrow.compute functions, and not many of those are actually implemented for the newer string_view data type on the pyarrow side.
I don't know if we should warn users about that when the construct a dataframe with string_view.. Or maybe we should actually also consider still by default convert string_view to string, given those usability issues (the question then is mostly how to let the user actually ask for allowing string_view explicitly, if by default we would still convert)
Or maybe we should actually also consider still by default convert
string_viewtostring
I'd be hesitant to do this without clarifying how we expect logical types to behave. I think that would also be the exact opposite of what polars does (i.e. they convert string to string_view) so that would lead to some fragmentation in expectations