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 Python sequence of datetime-like objects by seisman · Pull Request #3758 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Jan 9, 2025 •
edited by yvonnefroehlich
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Description of proposed changes

This PR adds tests for Python sequence (list or tuple) of datetime-like objects, which usually can't be converted to np.datetime64 dtype directly by np.ascontiguousarray.

Tested datetime-like objects include:

  • ISO8601 strings
  • Python datetime/date objects
  • np.datetime64
  • pd.Timestamp
  • mixed types

Need to need that, this PR still can't convert a sequence of pyarrow timestamp to np.datetime64, as shown below:

In [1]: import numpy as np

In [2]: import pyarrow as pa

In [3]: import datetime

In [4]: array =  [
   ...:     pa.scalar(datetime.datetime(2018, 1, 1)),
   ...:     pa.scalar(datetime.datetime(2018, 2, 1)),
   ...:     pa.scalar(datetime.datetime(2018, 3, 1)),
   ...:     pa.scalar(datetime.datetime(2018, 4, 1, 1, 2, 3)),
   ...: ]

In [5]: array
Out[5]: 
[<pyarrow.TimestampScalar: '2018-01-01T00:00:00.000000'>,
 <pyarrow.TimestampScalar: '2018-02-01T00:00:00.000000'>,
 <pyarrow.TimestampScalar: '2018-03-01T00:00:00.000000'>,
 <pyarrow.TimestampScalar: '2018-04-01T01:02:03.000000'>]

In [6]: np.ascontiguousarray(array)
Out[6]: 
array([<pyarrow.TimestampScalar: '2018-01-01T00:00:00.000000'>,
       <pyarrow.TimestampScalar: '2018-02-01T00:00:00.000000'>,
       <pyarrow.TimestampScalar: '2018-03-01T00:00:00.000000'>,
       <pyarrow.TimestampScalar: '2018-04-01T01:02:03.000000'>],
      dtype=object)

In [7]: np.ascontiguousarray(array, dtype=np.datetime64)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[7], line 1
----> 1 np.ascontiguousarray(array, dtype=np.datetime64)

ValueError: Could not convert object to NumPy datetime

Preview for affected tutorial: https://pygmt-dev--3758.org.readthedocs.build/en/3758/tutorials/advanced/date_time_charts.html#using-python-s-datetime

Sorry, something went wrong.

Uh oh!

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

All reactions
@seisman
clib.conversion._to_numpy: Add tests for arrays with Python built-in …
949b124
…datetimes or ISO-strings
@weiji14

weiji14 commented Jan 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Can confirm that this patch should fix the incorrect tutorial plot reported in #3757 (comment). See https://pygmt-dev--3758.org.readthedocs.build/en/3758/tutorials/advanced/date_time_charts.html#using-python-s-datetime

image

However, it doesn't fix the list[pd.Timestamp] bug originally reported in #3757 (comment)? Edit: Sorry, it does fix the bug, accidentally checked out the wrong branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

weiji14
weiji14 reviewed Jan 9, 2025
View reviewed changes
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 Resolved

Uh oh!

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

seisman and others added 2 commits January 9, 2025 09:58
@seisman
Merge branch 'main' into to_numpy/datetime
ada31d8
@seisman @weiji14
Add more tests by Wei Ji
0eba5d2
Co-authored-by: Wei Ji <[email protected]>
@seisman seisman added the maintenance Boring but important stuff for the core devs label Jan 9, 2025
seisman added 2 commits January 9, 2025 10:39
@seisman
Merge branch 'main' into to_numpy/datetime
738ed3c
@seisman
Add tests for numpy datetime64
3aa5233
@seisman seisman changed the title clib.conversion._to_numpy: Add tests for arrays with Python built-in datetimes or ISO-strings clib.conversion._to_numpy: Add tests for arrays with Python sequence of datetime-like objects Jan 9, 2025
@seisman seisman changed the title clib.conversion._to_numpy: Add tests for arrays with Python sequence of datetime-like objects clib.conversion._to_numpy: Add tests for Python sequence of datetime-like objects Jan 9, 2025
@seisman
seisman marked this pull request as ready for review January 9, 2025 02:51
@seisman

seisman commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

This PR is now ready for review. I feel we should split this PR into two small PRs, one labelled as "bug", and another one labeled as "maintenance".

I guess we also need to release v0.14.1.

All reactions

Sorry, something went wrong.

Uh oh!

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

@weiji14

weiji14 commented Jan 9, 2025

Copy link
Copy Markdown
Member

I'm looking into why our unit tests didn't catch the bug at #3757, these lines in test_plot_datetime actually uses a list[datetime.datetime] object:

pygmt/pygmt/tests/test_plot.py

Lines 469 to 472 in 9e912ba

# the Python built-in datetime and date
x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)]
y = [8.5, 9.5]
fig.plot(x=x, y=y, style="i0.2c", pen="1p")

Maybe we should extend the unit test to include pandas.Timestamp? E.g. adding these lines:

    # list of pandas.Timestamp
    x = [pd.Timestamp("2020-1-1"), pd.Timestamp("2021-1-1")]
    y = [10.5, 11.5]
    fig.plot(x=x, y=y, style="h0.2c", pen="1p")

I guess we also need to release v0.14.1.

Yes, wish we hadn't merged some of those deprecation PRs yet 🙂 We could either revert those, or branch off of commit b1c984a to make a release perhaps? Maybe start a new issue for this.

All reactions

Sorry, something went wrong.

Uh oh!

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

@seisman

seisman commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

Here is the callstack: virtualfile_from_vectors->vectors_to_arrays->_to_numpy.

The issue is that, any datetime-like Python sequences will be converted to np.object_ dtype, then the following codes convert np.object_ to np.str_:

pygmt/pygmt/clib/conversion.py

Lines 208 to 211 in 9e912ba

# Check if a np.object_ array can be converted to np.str_.
if array.dtype == np.object_:
with contextlib.suppress(TypeError, ValueError):
return np.ascontiguousarray(array, dtype=np.str_)

So, we're actually passing datetime strings into GMT. In the test_plot_datetime test, x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)] is converted to strings array ["2018-01-01", "2019-01-01"], which can be recognized by GMT, but in our docs and the pd.timestamp case, the array is converted to a string array with whitespaces (e.g., ['2024-12-01 00:00:00', '2024-12-15 00:00:00']), so it can't be recognized by GMT.

All reactions

Sorry, something went wrong.

Uh oh!

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

@seisman

seisman commented Jan 9, 2025 •
edited by weiji14
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Maybe we should extend the unit test to include pandas.Timestamp? E.g. adding these lines:

    # list of pandas.Timestamp
    x = [pd.Timestamp("2020-1-1"), pd.Timestamp("2021-1-1")]
    y = [10.5, 11.5]
    fig.plot(x=x, y=y, style="h0.2c", pen="1p")

See the explanations above. As long as _to_numpy can convert a datetime-like object into np.datetime64 array correctly, PyGMT should be able to plot/process it. So, I think the newly added tests are enough.

All reactions

Sorry, something went wrong.

Uh oh!

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

@seisman seisman added this to the 0.15.0 milestone Jan 9, 2025
This was referenced Jan 9, 2025
Supported data types by the private _to_numpy function #3600
Open
Release PyGMT v0.14.1 #3759
Closed
@seisman seisman added the needs review This PR has higher priority and needs review. label Jan 9, 2025
seisman added 2 commits January 9, 2025 15:26
@seisman
Merge branch 'main' into to_numpy/datetime
8bb416d
@seisman
Convert unrecognized objects to datetime before converting to string …
faaa45c
…dtype
@seisman seisman mentioned this pull request Jan 9, 2025
Fix the bug of converting Python sequence of datetime-like objects #3760
Merged
@seisman

seisman commented Jan 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

This PR is now ready for review. I feel we should split this PR into two small PRs, one labelled as "bug", and another one labeled as "maintenance".

I've opened a separate PR at #3760 and also changed the base branch to fix/datetime.

weiji14 and yvonnefroehlich reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@seisman
seisman changed the base branch from main to fix/datetime January 9, 2025 11:15
@seisman seisman added the skip-changelog Skip adding Pull Request to changelog label Jan 9, 2025
@weiji14 weiji14 modified the milestones: 0.15.0, 0.14.1 Jan 9, 2025
@seisman
Add tests for sequence of pd.Timestamp
892f444
@github-actions

github-actions Bot commented Jan 10, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Summary of changed images

This is an auto-generated report of images that have changed on the DVC remote

Status Path
modified pygmt/tests/baseline/test_plot_datetime.png

Image diff(s)

Details

Added images

Modified images

Path Old New
test_plot_datetime.png

Report last updated at commit 8b8767e

All reactions

Sorry, something went wrong.

Uh oh!

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

@seisman
Merge branch 'fix/datetime' into to_numpy/datetime
cdec182
michaelgrund
michaelgrund approved these changes Jan 10, 2025
View reviewed changes
@seisman seisman 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 Jan 11, 2025
seisman added 3 commits January 11, 2025 21:47
@seisman
Merge branch 'main' into fix/datetime
2aa1e4e
@seisman
Merge branch 'main' into fix/datetime
b3c25f8
@seisman
Merge branch 'fix/datetime' into to_numpy/datetime
cdc1d54
Base automatically changed from fix/datetime to main January 13, 2025 00:11
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Jan 13, 2025
@seisman
Merge branch 'main' into to_numpy/datetime
8b8767e
@seisman
seisman merged commit 900e8d4 into main Jan 13, 2025
@seisman
seisman deleted the to_numpy/datetime branch January 13, 2025 00:22
seisman added a commit that referenced this pull request Jan 17, 2025
@seisman @weiji14
clib.conversion._to_numpy: Add tests for Python sequence of datetime-…
2d090ee
…like objects (#3758)

Co-authored-by: Wei Ji <[email protected]>
@seisman seisman mentioned this pull request Jan 17, 2025
clib.conversion._to_numpy: Add tests for Python sequence of datetime-like objects (#3758) #3776
Merged
seisman added a commit that referenced this pull request Jan 21, 2025
@seisman @weiji14
clib.conversion._to_numpy: Add tests for Python sequence of datetime-…
d6a763e
…like objects (#3758) (#3776)

Co-authored-by: Wei Ji <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@weiji14 weiji14 weiji14 left review comments
@michaelgrund michaelgrund michaelgrund 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.1

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 @weiji14 @michaelgrund
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 01402775 — 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.