Repository navigation
Description
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
import pyarrow as pa
pyarrow_list_of_strings = pd.ArrowDtype(pa.list_(pa.string()))
data = pd.DataFrame({
"Pyarrow" : pd.Series([["a"], ["a", "b"]], dtype = pyarrow_list_of_strings),
})
data.to_parquet("data.parquet") # SUCCESS
pd.read_parquet("data.parquet") # *** FAIL
data_object = pd.DataFrame({
"Pyarrow" : pd.Series([["a"], ["a", "b"]], dtype = object),
})
data_object.to_parquet("data.parquet")
pyarrow_internal = pa.parquet.read_table("data.parquet") # SUCCESS with type list[string]
pyarrow_internal .to_pandas() # SUCCESS except object now
pd.Series(pd.arrays.ArrowExtensionArray(pyarrow_internal["Pyarrow"])) # SUCCESS - data-type also correct!Issue Description
Great work on extending Arrow to Pandas!
Using pd.ArrowDtype(pa.list_(pa.string())) or any other alteration works in the Parquet saving mode, but fails during the reading of the parquet file.
In fact, if there is a Pandas Series of pure lists of strings for eg ["a"], ["a", "b"], Parquet saves it internally as a list[string] type. When Pandas reads the parquet file, it then converts it to an object type.
Is there a way during the reading step to either:
- Convert the data-type like in the pure list mode to an
objecttype OR pd.Series(pd.arrays.ArrowExtensionArray(x))seems to actually work! Maybe during the conversion from the internal Pyarrow representation into Pandas, we can usepd.Series(pd.arrays.ArrowExtensionArray(x))on columns which had errors? OR- Somehow support these new types?
Expected Behavior
import pandas as pd
import pyarrow as pa
pyarrow_list_of_strings = pd.ArrowDtype(pa.list_(pa.string()))
data = pd.DataFrame({
"Pyarrow" : pd.Series([["a"], ["a", "b"]], dtype = pyarrow_list_of_strings),
})
data.to_parquet("data.parquet") # SUCCESS
pd.read_parquet("data.parquet") # SUCCESS
Installed Versions
Details
INSTALLED VERSIONS
commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 30 Stepping 5, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Australia.1252
pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : 0.29.34
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0rc1
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : 0.21.0
tzdata : 2023.3
qtpy : None
pyqt5 : None
Activity
danielhanchen commentedon May 1, 2023
I found during the Pyarrow conversion, if you pass in a
types_mapperand settingignore_metadatatoFalse, it works!mroeschke commentedon May 1, 2023
From the traceback, it appears that pyarrow tries to convert this type to a numpy dtype by default, so I think an appropriate fix would be for pyarrow to just return an
ArrowDtypehere[-]BUG: TypeError: data type 'list<item: string>[pyarrow]' not understood[/-][+]BUG: read_parquet converts pyarrow list type to numpy dtype[/+]danielhanchen commentedon May 2, 2023
Hmm so I looked at the Pandas code, and not sure if using
pd.ArrowDtype(dtype)will work.The issue is
data.schema.pandas_metadata['columns'][7]["numpy_type"]is astrand not an actualtypeobject, and pd.ArrowDtype does not accept strings.eg:
returns:
and using
fails since it's a string.
I think the better approach would be to not just pass in
data.schema.pandas_metadata['columns'][j]["numpy_type"]but alsodata.schema.typessince it has the actual types which can be converted into apd.ArrowDtypeobject.phofl commentedon May 2, 2023
I think this behaves as expected. You can pass
dtype_backend="pyarrow"to keep the list dtypedanielhanchen commentedon May 3, 2023
@phofl
Oh oops I forgot to mention I tried
pd.read_parquet(..., dtype_backend = "pyarrow"), and theTypeErrorstill exists. The error is exactly the same, since it passes the dtype tonp.dtypedanielhanchen commentedon May 3, 2023
Confirmed it still fails:
phofl commentedon May 3, 2023
Interesting,
This one works:
danielhanchen commentedon May 3, 2023
Ye that works since it's an
object- Pyarrow indeed saves the data inside the parquet file aslist[string].The issue is if you explicity parse
list[string]directly, it does not work.Ie:
returns
16 remaining items
giftculture commentedon Feb 8, 2024
I'm running into this issue as well:
giftculture commentedon Feb 8, 2024
INSTALLED VERSIONS
commit : f538741
python : 3.11.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.13.9-300.fc27.x86_64
Version : #1 SMP Mon Oct 23 13:41:58 UTC 2017
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.4
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.9
jinja2 : 3.1.3
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : 2023.10.1
fsspec : 2023.12.2
gcsfs : None
matplotlib : 3.8.2
numba : 0.58.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : 2.0.25
tables : None
tabulate : None
xarray : 2024.1.1
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : 2.4.1
pyqt5 : None
jborman-stonex commentedon Apr 4, 2024
The only "workaround" at the pandas-level I've found is to set
df.to_parquet(..., store_schema=False)for adfcontaining complex/nested types like a categorical. Are there any plans to get successful dtype roundtripping going forward? Is there anything in the pyarrow library we can leverage here?Versions:
Details
phofl commentedon Apr 4, 2024
I would recommend using
dtype_backend="pyarrow"giftculture commentedon Apr 4, 2024
@phoff, not sure if you saw from my screenshot, but I did apply the dypte_backend="pyarrow" to the read_parquet method and it still fails, unless I am misunderstanding your suggestion
bretttully commentedon Apr 4, 2024
The only workaround I have found so far is the following (which works in all cases I have thought of, except round-tripping an empty dataframe with a struct or list type, setting the schema, and not using
dtype_backend="pyarrow"when reading back in).Would def welcome suggested improvements to this workaround!
Obv you can write this differently if you don't want a byte string returned, but for us that's what we want.
kinianlo commentedon May 21, 2024
This bug is very similar to #57411 where the data type is list[int] instead of list[str].
judahrand commentedon Sep 3, 2024
Additionally,
list<element:can also belist<item:depending on how the Parquet was written out (see:use_compliant_nested_typeargument to pyarrow.parquet.ParquetWriter). And within Arrow itself the containing name be arbitrary (ie.pyarrow.list_(pyarrow.field('some_name', pyarrow.int64()))is a valid type). So the replace would have to be a regex and something liker'list<(.+):'with the capturing group grabbing the name of the field.Reorder to_pandas extension dtype mapping
bretttully commentedon Nov 14, 2024
Matching arrow ticket: apache/arrow#39914 and potential PR: apache/arrow#44720
jorisvandenbossche commentedon Nov 14, 2024
Only seeing this long ticket now .. FWIW I think this is another good reason it would be good pandas had tighter control over the pandas<->arrow conversion (#59780)
GH-39914: [pyarrow] Reorder to_pandas extension dtype mapping (#44720)
GH-39914: [pyarrow] Reorder to_pandas extension dtype mapping (#44720)
mikeoconnor0308 commentedon Aug 8, 2025
Is there any update on this ticket? It seems to still be a problem on pyarrow 21.0.0, despite apache/arrow#39914