Repository navigation
Closed
Labels
bugSomething is brokenneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.parquetupstream
Description
git bisect traced this back to #9131:
import dask.dataframe as dd, pandas as pd
dd.from_pandas(pd.DataFrame({"a": [1, 2]}, index=pd.Index(["A", "B"], dtype="string")), npartitions=1).to_parquet("/tmp/to_parquet_test/")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-d12a786a20e0> in <module>
1 import dask.dataframe as dd, pandas as pd
----> 2 dd.from_pandas(pd.DataFrame({"a": [1, 2]}, index=pd.Index(["A", "B"], dtype="string")), npartitions=1).to_parquet("/tmp/to_parquet_test/")
~/model/.venv/lib/python3.9/site-packages/dask/dataframe/core.py in to_parquet(self, path, *args, **kwargs)
~/model/.venv/lib/python3.9/site-packages/dask/dataframe/io/parquet/core.py in to_parquet(df, path, engine, compression, write_index, append, overwrite, ignore_divisions, partition_on, storage_options, custom_metadata, write_metadata_file, compute, compute_kwargs, schema, name_function, **kwargs)
~/model/.venv/lib/python3.9/site-packages/dask/dataframe/io/parquet/arrow.py in initialize_write(cls, df, fs, path, append, partition_on, ignore_divisions, division_info, schema, index_cols, **kwargs)
~/model/.venv/lib/python3.9/site-packages/dask/utils.py in __call__(self, arg, *args, **kwargs)
~/model/.venv/lib/python3.9/site-packages/dask/dataframe/backends.py in get_pyarrow_schema_pandas(obj)
~/model/.venv/lib/python3.9/site-packages/pyarrow/types.pxi in pyarrow.lib.Schema.from_pandas()
~/model/.venv/lib/python3.9/site-packages/pyarrow/pandas_compat.py in dataframe_to_types(df, preserve_index, columns)
527 type_ = pa.array(c, from_pandas=True).type
528 elif _pandas_api.is_extension_array_dtype(values):
--> 529 type_ = pa.array(c.head(0), from_pandas=True).type
530 else:
531 values, type_ = get_datetimetz_type(values, c.dtype, None)
AttributeError: 'Index' object has no attribute 'head'
Removing dtype="string" fixes the issue.
I can't quite make out whether the mistaken assumption is in dask or pyarrow...? But regardless since it worked prior to #9131 it seems like dask should be able to work around the issue.
cc @jcrist
Metadata
Metadata
Assignees
Labels
bugSomething is brokenneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.parquetupstream
Activity
jrbourbeau commentedon Jun 14, 2022
Thanks for surfacing @bnaul. I'm able to reproduce using the latest
pyarrow=8.0.0andpandas=1.4.2releases (with the currentdaskmainbranch).I'm able to reproduce with
pyarrowalone (see snippet below) which makes me think the underlying issue is related topyarrows handling of DataFrame's whose index are using extension dtypes. cc @jorisvandenbossche for visibilityThis is also true when just using
pyarrowjrbourbeau commentedon Jun 14, 2022
Have a possible workaround I'll push up in a bit...
bnaul commentedon Jun 14, 2022
Thanks @jrbourbeau, I am seeing that passing
schema=Noneseems to revert to the old behavior, so I guess the change to that default argument is really what surfaced this.jrbourbeau commentedon Jun 14, 2022
So it turns out while
doesn't work, creating a
pyarrow.Tableand then extracting the schema withdoes kind of work. It doesn't raise an exception but also doesn't preserve the
string[python]extension dtype for the index. Instead it converts toobject.ian-r-rose commentedon Jun 15, 2022
Reported upstream here. I think changing to
schema=None(or supplying a manual schema toddf.to_parquet) is the correct workaround until it is fixed upstream.jrbourbeau commentedon Sep 28, 2022
Closing this issue as resolved by apache/arrow#14080. When using the latest nightly version of
pyarrowthe original example now passes. Thanks for reporting @bnaul