-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: pyarrow dtype_backend incorrectly loads columns (from parquet) when the data stored is a list of structs and one of the struct fields has only None/null values #58867
Comments
Thanks for the report - confirmed on main. It seems like we are in an invalid state here - |
take |
The issue seems to stem from the upstream pyarrow package, specifically within the cast method of ChunkedArray. import pyarrow as pa
from pandas import ArrowDtype
from pandas.core.arrays.arrow.array import ArrowExtensionArray
fields = [
('b', pa.null()),
('g', pa.string()),
]
# Creating ArrowExtensionArray from ChunkedArray directly works
arr = pa.array([[{'b': None, 'g': None}, {'b': None, 'g': 'moo'}]], type=pa.list_(pa.struct(fields)))
carr = pa.chunked_array(arr)
data = ArrowExtensionArray(carr)
print(data) # Works
print(data[0][0]) # Works
# ChunkedArray cast causes issue
pandas_dtype = ArrowDtype(arr.type)
ext_arr = carr.cast(pandas_dtype.pyarrow_dtype, safe=True)
print(ext_arr) # prints Invalid array but no exception
print(len(ext_arr)) # Len is 1
print(ext_arr[0]) # Raises ArrowIndexError The snippet fails with:
|
Thanks @Angel-212 - stripping pandas from your reproducer: fields = [('b', pa.null()),('g', pa.string())]
dtype = pa.list_(pa.struct(fields))
arr = pa.array([[{'b': None, 'g': None}, {'b': None, 'g': 'moo'}]], type=dtype)
carr = pa.chunked_array(arr)
ext_arr = carr.cast(dtype, safe=True)
print(ext_arr) # prints Invalid array but no exception
print(len(ext_arr)) # Len is 1
print(ext_arr[0]) # Raises ArrowIndexError Makes this a pyarrow issue. @Angel-212 - would you be able to open an issue with PyArrow? |
Submitted apache/arrow#43838 |
Going to leave this issue open to verify whether it gets fixed once the above issue is resolved. |
Pandas version checks
I have checked that this issue has not already been reported.
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
Issue Description
In this scenario the column data is reconstructed such that the underlying list object is invalid, ie somehow the list is created such that it contains all the structs as 1 element in the list rather than N while the list metadata thinks there are N elements.
It's unclear to me whether this is due to the implementation of
pd.ArrowDType
or the way it is called by the pyarrow Tableto_pandas
method when it is used as atypes_mapper
.I've been able to only reproduce this issue when one of the struct fields only has None values defined and the dtype is inferred as
null
, e.g. in the example the column dtype islist<element: struct<b: null, g: string>>[pyarrow]
Expected Behavior
The expected behavior is shown in the example above when using the default
dtype_backend
.Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.11.8.final.0
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 : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
setuptools : 69.2.0
pip : 24.0
Cython : 3.0.9
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.3.1
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 16.1.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.3.1
scipy : 1.12.0
sqlalchemy : 2.0.28
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: