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: Remove the unused array_to_datetime function by seisman · Pull Request #3507 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Oct 11, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

After a series of PRs (especially #3760), the _to_numpy function takes the responsibility to convert any array-like objects into numpy arrays with recognized dtypes. So, if _to_numpy returns a np.object dtype string, it means the original array cannot to converted to a numpy array that can be passed to GMT.

After #3760, the array_to_datetime function is no longer used and can be removed.

Sorry, something went wrong.

Uh oh!

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

All reactions
@seisman seisman added the run/benchmark Trigger the benchmark workflow in PRs label Oct 11, 2024
@seisman
seisman force-pushed the remove/array-to-datetime branch from e5cf4f1 to 705aa4a Compare October 13, 2024 14:57
@seisman
Refactor vectors_to_arrays and deprecate the array_to_datetime function
3661e54
@seisman
seisman force-pushed the remove/array-to-datetime branch from 705aa4a to d7503d1 Compare October 13, 2024 15:31
@seisman
No need to use pd.api.types.is_string_dtype anymore
20b9215
@seisman
seisman force-pushed the remove/array-to-datetime branch from d7503d1 to 20b9215 Compare October 13, 2024 15:47
seisman
seisman commented Oct 13, 2024
View reviewed changes
Comment thread pygmt/clib/session.py Outdated
# latitude, or datetime string types.
for col, array in enumerate(arrays[2:]):
if pd.api.types.is_string_dtype(array.dtype):
if array.dtype.type == np.str_:

@seisman seisman Oct 13, 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.

pd.api.types.is_string_dtype was added in PR #684. Before PR #684, we used np.issubdtype(array.dtype, np.str_), which was added in #520. Any specific reason that we should use np.issubdtype(array.dtype, np.str_), not array.dtype.type == np.str_?

Sorry, something went wrong.

Uh oh!

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

All reactions

@weiji14 weiji14 Oct 14, 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.

I think we'll need to check if this can handle pandas.StringDtype and pyarrow.StringArray (xref #2933).

Sorry, something went wrong.

Uh oh!

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

All reactions

@seisman seisman Oct 14, 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.

Both can be converted to the numpy string dtype by the vectors_to_arrays method, so in virtualfile_from_vectors, we only need to check np.str_:

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: import pyarrow as pa

In [4]: x = pd.Series(["abc", "defghi"], dtype="string")

In [5]: np.asarray(x)
Out[5]: array(['abc', 'defghi'], dtype=object)

In [6]: np.asarray(x, dtype=str)
Out[6]: array(['abc', 'defghi'], dtype='<U6')

In [7]: y = pa.array(["abc", "defghi"])

In [8]: np.asarray(y)
Out[8]: array(['abc', 'defghi'], dtype=object)

In [9]: np.asarray(y, dtype=str)
Out[9]: array(['abc', 'defghi'], dtype='<U6')

Sorry, something went wrong.

Uh oh!

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

All reactions

@seisman seisman Oct 14, 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.

The main idea of this PR is to let vectors_to_arrays do the conversion work from any dtypes (including pd.StringDtype and pa.StringArray) into the numpy dtypes, so that the virtualfile_from_vectors only need to care about how to map numpy dtypes into GMT data types.

For any special dtypes that we know how to convert it to numpy dtype, we can maintain a mapping dictionary, just like what you did to support pyarrow's date32[day] and date64[ms] in #2845:

pygmt/pygmt/clib/conversion.py

Lines 208 to 211 in c2e429c

dtypes = {
"date32[day][pyarrow]": np.datetime64,
"date64[ms][pyarrow]": np.datetime64,
}

Sorry, something went wrong.

Uh oh!

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

All reactions

@seisman seisman Oct 14, 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.

In 83673cf, I've moved most of the doctests into a separate test file pygmt/tests/test_clib_vectors_to_arrays.py. A test test_vectors_to_arrays_pandas_string is added to check that the function can handle pd.StringDtype correctly.

Sorry, something went wrong.

Uh oh!

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

All reactions

@seisman seisman Oct 14, 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.

For any special dtypes that we know how to convert it to numpy dtype, we can maintain a mapping dictionary, just like what you did to support pyarrow's date32[day] and date64[ms] in #2845:

pygmt/pygmt/clib/conversion.py

Lines 208 to 211 in c2e429c

dtypes = {
"date32[day][pyarrow]": np.datetime64,
"date64[ms][pyarrow]": np.datetime64,
}

Based on the tests below, I think we should add the entry "string": np.str_ to the dictionary:

In [1]: import pandas as pd

In [2]: x = pd.Series(["abc", "12345"])

In [3]: x.dtype
Out[3]: dtype('O')

In [4]: str(x.dtype)
Out[4]: 'object'

In [5]: x = pd.Series(["abc", "12345"], dtype="string")

In [6]: x.dtype
Out[6]: string[python]

In [7]: str(x.dtype)
Out[7]: 'string'

In [8]: x = pd.Series(["abc", "12345"], dtype="string[pyarrow]")

In [9]: x.dtype
Out[9]: string[pyarrow]

In [10]: str(x.dtype)
Out[10]: 'string'

In [11]: import pyarrow as pa

In [12]: x = pa.array(["abc", "defghi"])

In [13]: x.type
Out[13]: DataType(string)

In [14]: str(x.type)
Out[14]: 'string'

Sorry, something went wrong.

Uh oh!

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

All reactions

@seisman seisman Oct 14, 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.

In PR #2774 and #2774, we only checked if PyGMT supports pandas with the pyarrow backend, but didn't check if the original pyarrow arrays works. For example, for a pyarrow date32 array, we need to check array.type rather than array.dtype:

In [1]: import datetime

In [2]: import pyarrow as pa

In [3]: x = pa.array([datetime.date(2020, 1, 1), datetime.date(2021, 12, 31)])

In [4]: str(x.type)
Out[4]: 'date32[day]'

Sorry, something went wrong.

Uh oh!

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

All reactions

@weiji14 weiji14 Oct 21, 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.

For example, for a pyarrow date32 array, we need to check array.type

Yes, raw pyarrow arrays of date32/date64 type are not supported yet. That's why it's marked 🚧 in #2800 (I was planning to modify array_to_datetime to handle it).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pygmt/clib/conversion.py Resolved

Uh oh!

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

@seisman seisman changed the title WIP: Refactor vectors_to_arrays and deprecate the array_to_datetime function clib.conversion: Deal with np.object dtype in vectors_to_arrays and deprecate the array_to_datetime function Oct 13, 2024
@seisman seisman added maintenance Boring but important stuff for the core devs needs review This PR has higher priority and needs review. labels Oct 13, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 13, 2024
@seisman
seisman marked this pull request as ready for review October 13, 2024 16:24
michaelgrund
michaelgrund approved these changes Oct 14, 2024
View reviewed changes
seisman added 3 commits October 14, 2024 18:02
@seisman
Add tests for vectors_to_arrays
83673cf
@seisman
Rename test_clib_conversion.py to test_clib_vectors_to_arrays.py
56a0841
@seisman
Add one line of code back
6338cde
@seisman
seisman force-pushed the remove/array-to-datetime branch from aa3ffc3 to 6338cde Compare October 14, 2024 10:47
seisman added 2 commits October 14, 2024 18:50
@seisman
Explicitly checking array.dtype.type
1864556
@seisman
Correctly skip the tests if pyarrow is not installed
54160bf
@seisman
seisman force-pushed the remove/array-to-datetime branch from a029191 to 54160bf Compare October 14, 2024 11:23
seisman added 2 commits October 14, 2024 20:15
@seisman
Explicitly specify how to convert pandas/pyarrow string dtype to nump…
f4e1a5f
…y string dtype
@seisman
Ensure the resulting numpy dtypes are supported by GMT
ed3be20
This was referenced Oct 14, 2024
pyarrow: Check compatibility of pyarrow.array with string type #2933
Merged
POC: Add private function _to_ndarray #3513
Closed
@seisman seisman removed the needs review This PR has higher priority and needs review. label Oct 15, 2024
@seisman
seisman marked this pull request as draft October 16, 2024 03:21
seisman added 3 commits October 17, 2024 16:48
@seisman
Merge branch 'main' into remove/array-to-datetime
8ab6c6c
@seisman
Revert any changes that enhances the conversion process
e26afbf
@seisman
Rename array_to_datetime to _array_to_datetime
be3c93e
@seisman
seisman marked this pull request as ready for review October 17, 2024 09:45
seisman added 2 commits November 15, 2024 15:13
@seisman
Merge branch 'main' into remove/array-to-datetime
9573fb6
@seisman
Remove the tests for pandas string dtypes
bccae94
@seisman seisman changed the title clib.conversion: Deal with np.object dtype in vectors_to_arrays and deprecate the array_to_datetime function clib.conversion: Remove the array_to_datetime function, which is not longer needed Nov 28, 2024
seisman added 2 commits November 28, 2024 15:28
@seisman
Merge branch 'main' into remove/array-to-datetime
d64c795
@seisman
Remove the array_to_datetime function
a410357
@weiji14 weiji14 changed the title clib.conversion: Remove the array_to_datetime function, which is not longer needed clib.conversion: Remove the array_to_datetime function, which is no longer needed Nov 28, 2024
This was referenced Nov 28, 2024
clib.conversion._to_numpy: Add tests for PyArrow's timestamp type #3621
Merged
How to handle datetime arrays with timezone information? #3656
Open
@seisman seisman removed this from the 0.14.0 milestone Dec 18, 2024
seisman added 2 commits January 9, 2025 15:27
@seisman
Merge branch 'main' into remove/array-to-datetime
53288dc
@seisman
Merge branch 'main' into remove/array-to-datetime
765d6c3
@seisman seisman added this to the 0.15.0 milestone Jan 13, 2025
@seisman seisman added skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. and removed run/benchmark Trigger the benchmark workflow in PRs labels Jan 13, 2025
seisman
seisman commented Jan 13, 2025
View reviewed changes
Comment thread pygmt/clib/session.py
Comment on lines -937 to -940
# For 1-D arrays, try to convert unknown object type to np.datetime64.
if ndim == 1 and array.dtype.type is np.object_:
with contextlib.suppress(ValueError):
array = array_to_datetime(array)

@seisman seisman Jan 13, 2025

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.

Moved to the _to_numpy function in PR #3760.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pygmt/clib/conversion.py
If the datetime string is invalid.

Examples
--------

@seisman seisman Jan 13, 2025

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.

These doctests are already covered by the tests in test_clib_to_numpy.py

Sorry, something went wrong.

Uh oh!

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

All reactions
@seisman
seisman marked this pull request as ready for review January 13, 2025 05:15
@seisman
Merge branch 'main' into remove/array-to-datetime
d847eee
@seisman
seisman requested a review from a team February 1, 2025 15:19
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Feb 1, 2025
@seisman seisman changed the title clib.conversion: Remove the array_to_datetime function, which is no longer needed clib.conversion: Remove the unused array_to_datetime function Feb 1, 2025
@seisman

seisman commented Feb 1, 2025

Copy link
Copy Markdown
Member Author

Ping @GenericMappingTools/pygmt-maintainers for final reviews. I plan to merge this PR on 2025/02/08 if there are no objections.

All reactions

Sorry, something went wrong.

Uh oh!

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

seisman added 2 commits February 3, 2025 18:49
@seisman
Merge branch 'main' into remove/array-to-datetime
b0d4029
@seisman
Merge branch 'main' into remove/array-to-datetime
9743bbf
@seisman
seisman merged commit 048bdc5 into main Feb 8, 2025
@seisman
seisman deleted the remove/array-to-datetime branch February 8, 2025 04:37
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Feb 8, 2025
@seisman seisman mentioned this pull request Feb 14, 2025
Release drafter only adds PRs merged since the latest release #3793
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@michaelgrund michaelgrund michaelgrund approved these changes
@weiji14 weiji14 weiji14 approved these changes

Uh oh!

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

Assignees

@seisman seisman

Labels

maintenance Boring but important stuff for the core devs

Projects

None yet

Milestone

0.15.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

@seisman @michaelgrund @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.