Skip to content

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

Merged
jorisvandenbossche merged 7 commits into
apache:mainfrom
bretttully:issue-53011
Nov 27, 2024
Merged

GH-39914: [pyarrow] Reorder to_pandas extension dtype mapping#44720
jorisvandenbossche merged 7 commits into
apache:mainfrom
bretttully:issue-53011

Conversation

@bretttully

@bretttullybretttully commented Nov 14, 2024

Copy link
Copy Markdown
Contributor

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.

Addresses pandas-dev/pandas#53011
`types_mapper` always had highest priority as it overrode what was set before. However, switching the logical ordering, it means that we don't need to call `_pandas_api.pandas_dtype(dtype)` when using the pyarrow backend. Resolving the issue of complex `dtype` with `list` or `struct`
@github-actions

Copy link
Copy Markdown

❌ GitHub issue #53011 could not be retrieved.

@github-actionsgithub-actionsBot added the awaiting review Awaiting review label Nov 14, 2024
@bretttullybretttully changed the title GH-53011: [pyarrow] Reorder to_pandas extension dtype mappingGH-39914: [pyarrow] Reorder to_pandas extension dtype mappingNov 14, 2024
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #39914has been automatically assigned in GitHub to PR creator.

@jorisvandenbossche

Copy link
Copy Markdown
Member

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)

Comment threadpython/pyarrow/tests/test_pandas.py Outdated
@bretttully

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown
Member

The test_dlpack failure in the tests you can ignore (#44728)

@github-actionsgithub-actionsBot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Nov 19, 2024

@jorisvandenbosschejorisvandenbossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
I triggered CI again

Comment threadpython/pyarrow/tests/test_pandas.py
@bretttully

Copy link
Copy Markdown
ContributorAuthor

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

@raulcd

Copy link
Copy Markdown
Member

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.

Comment threadpython/pyarrow/tests/test_pandas.py Outdated
@jorisvandenbossche

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g python

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Nov 20, 2024
@github-actions

Copy link
Copy Markdown

Revision: e3b9892

Submitted crossbow builds: ursacomputing/crossbow @ actions-e01b93275b

TaskStatus
example-python-minimal-build-fedora-condaGitHub Actions
example-python-minimal-build-ubuntu-venvGitHub Actions
test-conda-python-3.10GitHub Actions
test-conda-python-3.10-cython2GitHub Actions
test-conda-python-3.10-hdfs-2.9.2GitHub Actions
test-conda-python-3.10-hdfs-3.2.1GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-latestGitHub Actions
test-conda-python-3.10-substraitGitHub Actions
test-conda-python-3.11GitHub Actions
test-conda-python-3.11-dask-latestGitHub Actions
test-conda-python-3.11-dask-upstream_develGitHub Actions
test-conda-python-3.11-hypothesisGitHub Actions
test-conda-python-3.11-pandas-latest-numpy-1.26GitHub Actions
test-conda-python-3.11-pandas-latest-numpy-latestGitHub Actions
test-conda-python-3.11-pandas-nightly-numpy-nightlyGitHub Actions
test-conda-python-3.11-pandas-upstream_devel-numpy-nightlyGitHub Actions
test-conda-python-3.11-spark-masterGitHub Actions
test-conda-python-3.12GitHub Actions
test-conda-python-3.12-cpython-debugGitHub Actions
test-conda-python-3.13GitHub Actions
test-conda-python-3.9GitHub Actions
test-conda-python-3.9-pandas-1.1.3-numpy-1.19.5GitHub Actions
test-conda-python-emscriptenGitHub Actions
test-cuda-python-ubuntu-22.04-cuda-11.7.1GitHub Actions
test-debian-12-python-3-amd64GitHub Actions
test-debian-12-python-3-i386GitHub Actions
test-fedora-39-python-3GitHub Actions
test-ubuntu-22.04-python-3GitHub Actions
test-ubuntu-22.04-python-313-freethreadingGitHub Actions
test-ubuntu-24.04-python-3GitHub Actions

@github-actionsgithub-actionsBot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Nov 21, 2024
@jorisvandenbossche

Copy link
Copy Markdown
Member

@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

Copy link
Copy Markdown
Member

(the other failures are the known nightly dlpack failures)

@raulcd

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

@jorisvandenbossche

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
ContributorAuthor

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

@raulcd

Copy link
Copy Markdown
Member

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

@github-actions

Copy link
Copy Markdown

Revision: 685167f

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

TaskStatus
example-python-minimal-build-fedora-condaGitHub Actions
example-python-minimal-build-ubuntu-venvGitHub Actions

@bretttully

Copy link
Copy Markdown
ContributorAuthor

Is there anything else for me to do here?

@raulcd

Copy link
Copy Markdown
Member

Is there anything else for me to do here?

I don't think so. I am not comfortable with this area of our codebase so I'll let @jorisvandenbossche merge once he's happy about it, but as he already approved, he might do that soon.

@jorisvandenbossche

Copy link
Copy Markdown
Member

Is there anything else for me to do here?

No, just me getting back to merge it!

@jorisvandenbossche
jorisvandenbossche merged commit 8548c22 into apache:mainNov 27, 2024
@jorisvandenbosschejorisvandenbossche removed the awaiting change review Awaiting change review label Nov 27, 2024
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 8548c22.

There were 132 benchmark results with an error:

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 1 possible false positive for unstable benchmarks that are known to sometimes produce them.

@bretttully
bretttully deleted the issue-53011 branch November 27, 2024 23:29
@bretttully

Copy link
Copy Markdown
ContributorAuthor

🚀 Thanks for all your help here @jorisvandenbossche!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@bretttully@jorisvandenbossche@raulcd