GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — clib.conversion._to_numpy: Add tests for pandas.Series with pyarrow numeric dtypes by seisman · Pull Request #3585 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Nov 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR adds tests for pandas.Series with pyarrow numeric dtypes. Available pyarrow numeric dtypes are at https://arrow.apache.org/docs/python/api/datatypes.html.

Similar to the issue reported in #3584, there are also dtype conversion behavior changes in pandas 2.2.

With pandas>=2.2, everything works as expected:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: x = pd.Series([1, 2, 3], dtype="int32[pyarrow]")

In [4]: np.ascontiguousarray(x)
Out[4]: array([1, 2, 3], dtype=int32)

In [5]: x = pd.Series([1, pd.NA, 3], dtype="int32[pyarrow]")

In [6]: np.ascontiguousarray(x)
Out[6]: array([ 1., nan,  3.])

With pandas 2.1, missing values needs to convert to numpy float dtype explicitly.

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: x = pd.Series([1, 2, 3], dtype="int32[pyarrow]")

In [4]: np.ascontiguousarray(x)
Out[4]: array([1, 2, 3], dtype=int32)

In [5]: x = pd.Series([1, pd.NA, 3], dtype="int32[pyarrow]")

In [6]: np.ascontiguousarray(x)
Out[6]: array([1, <NA>, 3], dtype=object)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seisman seisman mentioned this pull request Nov 6, 2024
clib.conversion: Remove the unused array_to_datetime function #3507
Merged
@seisman
seisman force-pushed the to_numpy/pandas_numeric branch from 176f511 to eceff7f Compare November 6, 2024 14:18
@seisman
seisman force-pushed the to_numpy/pyarrow_dtypes branch 4 times, most recently from 2a2ab7a to 69b44ad Compare November 6, 2024 14:54
@seisman
seisman force-pushed the to_numpy/pandas_numeric branch 2 times, most recently from 6b77f42 to 7222db2 Compare November 7, 2024 00:17
@seisman
seisman force-pushed the to_numpy/pyarrow_dtypes branch from 69b44ad to 63df796 Compare November 7, 2024 13:43
@seisman
seisman changed the base branch from to_numpy/pandas_numeric to main November 7, 2024 13:43
@seisman
seisman marked this pull request as ready for review November 7, 2024 13:45
@seisman seisman changed the title WIP: clib.conversion._to_numpy: Add tests for pandas.Series with pyarrow numeric dtypes and pyarrow arrays clib.conversion._to_numpy: Add tests for pandas.Series and pyarrow.arrays with pyarrow numeric dtypes Nov 7, 2024
@seisman seisman changed the title clib.conversion._to_numpy: Add tests for pandas.Series and pyarrow.arrays with pyarrow numeric dtypes clib.conversion._to_numpy: Add tests for pandas.Series and pyarrow.array with pyarrow numeric dtypes Nov 7, 2024
@seisman
clib.conversion._to_numpy: Add tests for pandas.Series and pyarrow.ar…
a9b10d6
…ray with pyarrow numeric dtypes
@seisman
seisman force-pushed the to_numpy/pyarrow_dtypes branch from 63df796 to a9b10d6 Compare November 7, 2024 13:52
seisman
seisman commented Nov 7, 2024
View reviewed changes
Comment thread pygmt/tests/test_clib_to_numpy.py
# PyArrow dtypes can be specified using the following formats:
#
# - Prefixed with the name of the dtype and "[pyarrow]" (e.g., "int8[pyarrow]")
# - Specified using ``ArrowDType`` (e.g., "pd.ArrowDtype(pa.int8())")

@seisman seisman Nov 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We can't use pd.ArrowDtype(pa.int8()) here because pa is not defined when pyarrow is not installed. So we have to use the string aliases.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread pygmt/tests/test_clib_to_numpy.py Outdated
pytest.param("float64[pyarrow]", np.float64, id="float64[pyarrow]"),
],
)
def test_to_numpy_pandas_series_pyarrow_dtypes_numeric(dtype, expected_dtype):

@seisman seisman Nov 7, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This test is exactly the same as the test test_to_numpy_pandas_series_numpy_dtypes_numeric above, but I don't think we should merge them into a single test.

To merge them into a single test, we have to change the pytest.param to

pytest.param("float64[pyarrow]", np.float64, id="float64[pyarrow]", marks=skip_if_no(package="pyarrow")),

which is too long to fit in one line and will make the pytest.params too long to read.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weiji14 weiji14 Nov 11, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Managed to fit it into a single test like so:

from pygmt.helpers.testing import skip_if_no

pa_marks = {"marks": skip_if_no(package="pyarrow")}

@pytest.mark.parametrize(
    ("dtype", "expected_dtype"),
    [
        ...,
        pytest.param("int8[pyarrow]", np.int8, id="int8[pyarrow]", **pa_marks),
        pytest.param("int16[pyarrow]", np.int16, id="int16[pyarrow]", **pa_marks),
        pytest.param("int32[pyarrow]", np.int32, id="int32[pyarrow]", **pa_marks),
        pytest.param("int64[pyarrow]", np.int64, id="int64[pyarrow]", **pa_marks),
        pytest.param("uint8[pyarrow]", np.uint8, id="uint8[pyarrow]", **pa_marks),
        pytest.param("uint16[pyarrow]", np.uint16, id="uint16[pyarrow]", **pa_marks),
        pytest.param("uint32[pyarrow]", np.uint32, id="uint32[pyarrow]", **pa_marks),
        pytest.param("uint64[pyarrow]", np.uint64, id="uint64[pyarrow]", **pa_marks),
        pytest.param("float16[pyarrow]", np.float16, id="float16[pyarrow]", **pa_marks),
        pytest.param("float32[pyarrow]", np.float32, id="float32[pyarrow]", **pa_marks),
        pytest.param("float64[pyarrow]", np.float64, id="float64[pyarrow]", **pa_marks),
    ],
)

The longest line is just under 88 characters.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Nov 11, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Nice changes. We probably should merge this PR into #3584, so that we can test all pandas dtypes (including pyarrow-backed) in a single PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weiji14 weiji14 Nov 12, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sure, I see that this branch is targetting to_numpy/pandas_numeric/#3584, so you can merge this in then.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pygmt/tests/test_clib_to_numpy.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pygmt/tests/test_clib_to_numpy.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pygmt/tests/test_clib_to_numpy.py
# In PyArrow, array types can be specified in two ways:
#
# - Using string aliases (e.g., "int8")
# - Using pyarrow.DataType (e.g., ``pa.int8()``)

@seisman seisman Nov 7, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Again, we can't use pa.int8() here because pa is undefined if pyarrow is not installed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
seisman added 4 commits November 7, 2024 23:35
@seisman
Add workarounds for pandas nullable dtypes prior pandas v2.1
54d1a37
@seisman
Install pandas 2.0 + pyarrow in the Python 3.11 job
50e6872
@seisman
float32[pyarrow] with NA should be mapped to float32
1317e11
@seisman
Enable the test for float16[pyarrow]
e09aa75
@seisman
seisman force-pushed the to_numpy/pyarrow_dtypes branch from 70341af to e09aa75 Compare November 7, 2024 15:53
seisman added 2 commits November 8, 2024 00:11
@seisman
Use data.to_numpy(na_value=np.nan).astype(dtype=dtype) for float16
06e6958
@seisman
Add special handling for float16
94f335b
@seisman seisman mentioned this pull request Nov 8, 2024
clib.conversion._to_numpy: Add tests for pyarrow.array with pyarrow numeric types #3599
Merged
@seisman
seisman marked this pull request as draft November 8, 2024 02:49
@seisman
Remove tests for pyarrow.array. Already in PR #3599
bba7296
@seisman seisman changed the title clib.conversion._to_numpy: Add tests for pandas.Series and pyarrow.array with pyarrow numeric dtypes clib.conversion._to_numpy: Add tests for pandas.Series with pyarrow numeric dtypes Nov 8, 2024
This was referenced Nov 8, 2024
Supported data types by the private _to_numpy function #3600
Open
CI: Install pandas 2.1 + pyarrow in the Python 3.11 job #3605
Merged
@seisman
Merge branch 'main' into to_numpy/pyarrow_dtypes
f2e504b
@seisman
Change dtype to type for PyArrow
4150993
@seisman
seisman marked this pull request as ready for review November 9, 2024 08:56
@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Nov 9, 2024
@seisman seisman added this to the 0.14.0 milestone Nov 9, 2024
@seisman
Revert "Install pandas 2.0 + pyarrow in the Python 3.11 job"
b947b83
This reverts commit 50e6872.
@seisman
seisman changed the base branch from main to to_numpy/pandas_numeric November 10, 2024 04:49
@seisman
Merge branch 'to_numpy/pandas_numeric' into to_numpy/pyarrow_dtypes
0a82e3f
@seisman seisman removed the needs review This PR has higher priority and needs review. label Nov 10, 2024
seisman added 5 commits November 10, 2024 12:53
@seisman
Format
98dfb29
@seisman
Merge branch 'to_numpy/pandas_numeric' into to_numpy/pyarrow_dtypes
898bcca
@seisman
Install pandas 2.0 + pyarrow in the Python 3.11 job
10526da
@seisman
Merge branch 'to_numpy/pandas_numeric' into to_numpy/pyarrow_dtypes
f3bfef1
@seisman
Merge pyarrow-backed tests into pandas ones
c504bf2
@seisman seisman added the needs review This PR has higher priority and needs review. label Nov 11, 2024
@seisman
Fix a typo
cf397b7
weiji14
weiji14 approved these changes Nov 12, 2024
View reviewed changes
@seisman
seisman merged commit a65f6ae into to_numpy/pandas_numeric Nov 12, 2024
@seisman
seisman deleted the to_numpy/pyarrow_dtypes branch November 12, 2024 00:53
@seisman seisman removed the needs review This PR has higher priority and needs review. label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@weiji14 weiji14 weiji14 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog

Projects

None yet

Milestone

0.14.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@seisman @weiji14
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.