Skip to content

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

Open
@kbuma

Description

@kbuma

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

import pandas as pd

obj1 = {'a': [{'b': None}, 
              {'g': 'moo'}
              ]}

pd.DataFrame([obj1]).to_parquet("obj1.parq", engine="pyarrow")

a = pd.read_parquet("obj1.parq", engine="pyarrow")

print(a.a) 
print(a.a[0][0]) # success

a = pd.read_parquet("obj1.parq", engine="pyarrow", dtype_backend="pyarrow")

print(a.a) # note that the array is rendered as having one element instead of 2
print(a.a[0][0]) # ArrowIndexError

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 Table to_pandas method when it is used as a types_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 is list<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

Metadata

Metadata

Assignees

Labels

Arrowpyarrow functionalityBugIO Parquetparquet, featherUpstream issueIssue related to pandas dependency

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions