apache / arrow

Apache Arrow is the universal columnar format and multi-language toolbox for fast data interchange and in-memory analytics
https://arrow.apache.org/
Apache License 2.0
14.63k stars 3.56k forks source link

GH-39914: [pyarrow] Reorder to_pandas extension dtype mapping #44720

Open bretttully opened 1 week ago

bretttully commented 1 week ago

Rationale for this change

This is a long standing pandas ticket with some fairly horrible workarounds, where complex arrow types do not serialise well to pandas as the pandas metadata string is not parseable. However, types_mapper always had highest priority as it overrode what was set before.

What changes are included in this PR?

By switching the logical ordering, it means that we don't need to call _pandas_api.pandas_dtype(dtype) when using the pyarrow backend, thus resolving the issue of complex dtype with list or struct. It will likely still fail if the numpy backend is used, but at least this gives a working solution rather than an inability to load files at all.

Are these changes tested?

Existing tests should stay unchanged and a new test for the complex type has been added

Are there any user-facing changes?

This PR contains a "Critical Fix". This makes pd.read_parquet(..., dtype_backend="pyarrow") work with complex data types where the metadata added by pyarrow during pd.to_parquet is not serialisable and currently throwing an exception. This issue currently prevents the use of pyarrow as the default backend for pandas.

github-actions[bot] commented 1 week ago

:x: GitHub issue #53011 could not be retrieved.

github-actions[bot] commented 1 week ago

:warning: GitHub issue #39914 has been automatically assigned in GitHub to PR creator.

jorisvandenbossche commented 1 week ago

By switching the logical ordering, it means that we don't need to call _pandas_api.pandas_dtype(dtype) when using the pyarrow backend,

And because you added a name not in ext_columns to the subsequent methods to fill ext_columns, this should preserve the priority of the different methods to determine the pandas dtype? (metadata < pyarrow extension type < types_mapper)

bretttully commented 1 week ago

By switching the logical ordering, it means that we don't need to call _pandas_api.pandas_dtype(dtype) when using the pyarrow backend,

And because you added a name not in ext_columns to the subsequent methods to fill ext_columns, this should preserve the priority of the different methods to determine the pandas dtype? (metadata < pyarrow extension type < types_mapper)

Yes, exactly. Priority remains the same, but functions are skipped if the field already has a type, meaning that the code causing the error is no longer called if types_mapper is provided.

jorisvandenbossche commented 1 week ago

The test_dlpack failure in the tests you can ignore (https://github.com/apache/arrow/issues/44728)

bretttully commented 2 days ago

Thanks @jorisvandenbossche -- is the process that I can merge this following approval, or is that done by a core maintainer?

raulcd commented 2 days ago

is the process that I can merge this following approval, or is that done by a core maintainer?

A committer will merge, probably @jorisvandenbossche in this specific case, once everything is running and addressed. I've triggered CI for the latest changes.

jorisvandenbossche commented 1 day ago

@github-actions crossbow submit -g python

github-actions[bot] commented 1 day ago

Revision: e3b9892e5663f4888bc79c38b9d36bbefcdaf2b4

Submitted crossbow builds: ursacomputing/crossbow @ actions-e01b93275b

Task Status
example-python-minimal-build-fedora-conda GitHub Actions
example-python-minimal-build-ubuntu-venv GitHub Actions
test-conda-python-3.10 GitHub Actions
test-conda-python-3.10-cython2 GitHub Actions
test-conda-python-3.10-hdfs-2.9.2 GitHub Actions
test-conda-python-3.10-hdfs-3.2.1 GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.10-substrait GitHub Actions
test-conda-python-3.11 GitHub Actions
test-conda-python-3.11-dask-latest GitHub Actions
test-conda-python-3.11-dask-upstream_devel GitHub Actions
test-conda-python-3.11-hypothesis GitHub Actions
test-conda-python-3.11-pandas-latest-numpy-1.26 GitHub Actions
test-conda-python-3.11-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.11-pandas-nightly-numpy-nightly GitHub Actions
test-conda-python-3.11-pandas-upstream_devel-numpy-nightly GitHub Actions
test-conda-python-3.11-spark-master GitHub Actions
test-conda-python-3.12 GitHub Actions
test-conda-python-3.12-cpython-debug GitHub Actions
test-conda-python-3.13 GitHub Actions
test-conda-python-3.9 GitHub Actions
test-conda-python-3.9-pandas-1.1.3-numpy-1.19.5 GitHub Actions
test-conda-python-emscripten GitHub Actions
test-cuda-python-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-python-3-amd64 GitHub Actions
test-debian-12-python-3-i386 GitHub Actions
test-fedora-39-python-3 GitHub Actions
test-ubuntu-22.04-python-3 GitHub Actions
test-ubuntu-22.04-python-313-freethreading GitHub Actions
test-ubuntu-24.04-python-3 GitHub Actions
jorisvandenbossche commented 1 day ago

@raulcd it seems something is going wrong with the minimal test builds (eg example-python-minimal-build-fedora-conda). The logs indicate "Successfully installed pyarrow-0.1.dev16896+ge3b9892", which then messes up pandas detection of the pyarrow version (for the pyarrow integration in pandas, pandas checks if pyarrow is recent enough and otherwise errors), giving some test failures.

(but also not entirely sure how this PR causes this issue, since I don't see the nightlies fail for the minimal builds at the moment)

jorisvandenbossche commented 1 day ago

(the other failures are the known nightly dlpack failures)

raulcd commented 1 day ago

The logs indicate "Successfully installed pyarrow-0.1.dev16896+ge3b9892"

From the git checkout I see is pulling from the remote on Syncing repository: bretttully/arrow. I recall an issue if dev tags are not present we are unable to detect the correct version. The remote doesn't seem to have other branches and/or tags.

raulcd commented 1 day ago

I've opened an issue because we should find a way to not fail if the dev tag is not present:

jorisvandenbossche commented 1 day ago

Thanks for investigating that!

So then to resolve this here, @bretttully should fetch the upstream tags and push that to his fork? Something like

git fetch upstream
git push origin --tags

(assuming upstream is apache/arrow and origin is bretttully/arrow)

bretttully commented 1 day ago

I have merged upstream/main and pushed tags. Let's see if this works...

raulcd commented 23 hours ago

@github-actions crossbow submit example-python-minimal-build-*

github-actions[bot] commented 23 hours ago

Revision: 685167fb8dc28190f9fd8600bca5df7799663e5a

Submitted crossbow builds: ursacomputing/crossbow @ actions-524e782c26

Task Status
example-python-minimal-build-fedora-conda GitHub Actions
example-python-minimal-build-ubuntu-venv GitHub Actions