Skip to content

POC: Add private function _to_ndarray - #3513

Closed
seisman wants to merge 8 commits into
mainfrom
array_dtypes
Closed

POC: Add private function _to_ndarray#3513
seisman wants to merge 8 commits into
mainfrom
array_dtypes

Conversation

@seisman

@seismanseisman commented Oct 14, 2024

Copy link
Copy Markdown
Member

Description of proposed changes

Inspired by the discussions in #3507 (comment), I think it's necessary to understand the string representation of different dtypes and what dtype they are converted to by NumPy. This PR adds a private _array_dtypes function to pygmt/clib/conversion.py. Please refer to the detailed docstrings for what the function does.

I think we should include the function in PyGMT although it's not used anywhere, similar to the array_to_datetime function which will no longer be used in PyGMT after #3507. I'm open to comments and more array-like objects should be added for testing.

@seisman

Copy link
Copy Markdown
MemberAuthor

The CI tests pass with Python 3.12 + pandas 2.2 but fail with Python 3.10 + panda 2.0:

[gw0] linux -- Python 3.10.15 /home/runner/micromamba/envs/pygmt/bin/python3.10
444 >>> _array_dtypes(np.datetime64("2021-01-01"))
445 ('datetime64[D]', 'datetime64[D]')
446 447 For Pandas objects:
448 449 >>> _array_dtypes(pd.Series(data=[1, 2, 3]))
450 ('int64', 'int64')
451 >>> _array_dtypes(pd.Series(data=[1.0, 2.0, 3.0]))
452 ('float64', 'float64')
453 >>> _array_dtypes(pd.Series(data=[1, 2, 3], dtype=pd.Int32Dtype()))
Expected:
('Int32', 'int32')
Got:
('Int32', 'object')

There are likely some changes between pandas 2.0 and 2.2 (or in NumPy). Anyway, it means we need to maintain the mapping from Int32 to int32 to support old versions and it also highlights the importance of this private function.

Comment threadpygmt/clib/conversion.py Outdated
Comment threadpygmt/clib/conversion.py Outdated
@seismanseisman self-assigned this Oct 30, 2024
@seismanseisman changed the title POC: Add the private/unused _array_dtypes function to test dtype conversionsPOC: Add private function _to_ndarrayNov 1, 2024
@seisman
seismanforce-pushed the array_dtypes branch 3 times, most recently from ad61cc6 to 319880cCompareNovember 2, 2024 15:05
@seisman
seismanforce-pushed the array_dtypes branch 2 times, most recently from d7dc5ff to 0ef7d52CompareNovember 2, 2024 16:29
@seisman
seismanforce-pushed the array_dtypes branch 2 times, most recently from 6358b8c to e4807e2CompareNovember 3, 2024 09:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@seisman@michaelgrund