Skip to content

clib.converison._to_numpy: Add tests for pandas.Series with pandas numeric dtypes - #3584

Merged
seisman merged 46 commits into
mainfrom
to_numpy/pandas_numeric
Dec 12, 2024
Merged

clib.converison._to_numpy: Add tests for pandas.Series with pandas numeric dtypes#3584
seisman merged 46 commits into
mainfrom
to_numpy/pandas_numeric

Conversation

@seisman

@seismanseisman commented Nov 5, 2024

Copy link
Copy Markdown
Member

This PR adds tests for pandas.Series with pandas/pyarrow numeric dtypes (in 7222db2).

Tests pass with pandas 2.2 but fail with pandas 2.1. Check https://github.com/GenericMappingTools/pygmt/actions/runs/11714111582/job/32628172796?pr=3584 for details.

The failures are due to upstream pandas v2.2 changes. Previously, all pandas nullable dtypes are converted to np.object_ dtype. Since pandas v2.2, the new rules are (source: https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#to-numpy-for-numpy-nullable-and-arrow-types-converts-to-suitable-numpy-dtype):

  • float dtypes are cast to NumPy floats
  • integer dtypes without missing values are cast to NumPy integer dtypes
  • integer dtypes with missing values are cast to NumPy float dtypes and NaN is used as missing value indicator
  • boolean dtypes without missing values are cast to NumPy bool dtype
  • boolean dtypes with missing values keep object dtype
  • datetime and timedelta types are cast to Numpy datetime64 and timedelta64 types respectively and NaT is used as missing value indicator

Following the first three points, we add the workaround for pandas<2.2 to explicitly specify how to map pandas dtypes into numpy dtypes (a4f15cd). It is an improved version of PR #3505.

Here is the minimal example to understand the behavior change:

With pandas 2.1

In [1]: importnumpyasnpIn [2]: importpandasaspdIn [3]: x=pd.Series([1, 2, 3], dtype=pd.Int32Dtype())
In [4]: x.dtypeOut[4]: Int32Dtype()
In [5]: np.ascontiguousarray(x)
Out[5]: array([1, 2, 3], dtype=object)
In [6]: x=pd.Series([1, pd.NA, 3], dtype=pd.Int32Dtype())
In [7]: np.ascontiguousarray(x)
Out[7]: array([1, <NA>, 3], dtype=object)

With pandas 2.2

In [1]: importnumpyasnpIn [2]: importpandasaspdIn [3]: x=pd.Series([1, 2, 3], dtype=pd.Int32Dtype())
In [4]: np.ascontiguousarray(x)
Out[4]: array([1, 2, 3], dtype=int32)
In [5]: x=pd.Series([1, pd.NA, 3], dtype=pd.Int32Dtype())
In [6]: np.ascontiguousarray(x)
Out[6]: array([ 1., nan, 3.])

Related to #3581, #2848, #3513, #3583.

Wait for #3583.

@seisman
seismanforce-pushed the to_numpy/pandas_numeric branch from a7054b9 to c3471cbCompareNovember 5, 2024 11:03
@weiji14weiji14 changed the title WIP: clib.converison._to_numpy: Add tests for panda.Series with pandas numeric dtypesWIP: clib.converison._to_numpy: Add tests for pandas.Series with pandas numeric dtypesNov 6, 2024
@seisman
seismanforce-pushed the to_numpy/numpy_numeric branch from 8489ebe to 6f966dbCompareNovember 6, 2024 06:16
@seisman
seismanforce-pushed the to_numpy/pandas_numeric branch from 176f511 to eceff7fCompareNovember 6, 2024 14:18
seismanand others added 4 commits November 7, 2024 07:08
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman
seismanforce-pushed the to_numpy/pandas_numeric branch from eceff7f to 6b77f42CompareNovember 7, 2024 00:13
@seisman
seismanforce-pushed the to_numpy/pandas_numeric branch from 6b77f42 to 7222db2CompareNovember 7, 2024 00:17
@seisman

Copy link
Copy Markdown
MemberAuthor

As explained in the top post, currently this PR does two things:

  • Add tests for pandas dtypes
  • Improve the pandas->numpy dtype conversion for pd.Series with NA values (pandas<2.2)

After finishing this PR, we have two options:

  1. Change the PR title to something like "Improve the conversion of pandas dtypes with missing values for pandas<=2.1" and mark it as a "bug".
  2. Cherry-pick the changes in a4f15cd, and open a separate PR so that we'll have two commits in the main branch, one for the new workaround, and one for the newly added tests.

I'm inclined to option 2.

@weiji14

Copy link
Copy Markdown
Member

2. Cherry-pick the changes in a4f15cd, and open a separate PR so that we'll have two commits in the main branch, one for the new workaround, and one for the newly added tests.

I'm inclined to option 2.

Yeah, agree to option 2. Keep the unit tests here, and open a separate PR with the code changes from a4f15cd

Base automatically changed from to_numpy/numpy_numeric to mainNovember 7, 2024 10:38
@seismanseisman changed the title WIP: clib.converison._to_numpy: Add tests for pandas.Series with pandas numeric dtypesclib.converison._to_numpy: Add tests for pandas.Series with pandas numeric dtypesNov 7, 2024
@seisman
seisman marked this pull request as ready for review November 7, 2024 10:41
Comment threadpygmt/clib/conversion.py Outdated
Comment threadpygmt/clib/conversion.py Outdated
@seisman
seisman marked this pull request as ready for review November 15, 2024 05:27
@seismanseisman added the needs review This PR has higher priority and needs review. label Nov 15, 2024
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
@seismanseisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Dec 3, 2024
@seisman

Copy link
Copy Markdown
MemberAuthor

@weiji14 I'm wondering if you have time to give this PR a final review.

@seismanseisman removed the final review call This PR requires final review and approval from a second reviewer label Dec 12, 2024
@seisman
seisman merged commit 351247d into mainDec 12, 2024
@seisman
seisman deleted the to_numpy/pandas_numeric branch December 12, 2024 01:29
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenanceBoring but important stuff for the core devsskip-changelogSkip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@seisman@weiji14@michaelgrund@yvonnefroehlich