You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
plot no longer accepts date/time data in pygmt 0.14 #3757
The following script works in 0.13 (and before), but fails to plot anything in 0.14 with the message pygmt-session [WARNING]: Unable to parse 5 longitude strings:
So the xdata array get converted into a numpy array of type np.str_ instead of an np.datetime64 dtype, and is parsed incorrectly as GMT_TEXT instead of GMT_DATETIME here:
# 1-D arrays can be numeric or text, 2-D arrays can only be numeric.
valid_dtypes=DTYPESifndim==1elseDTYPES_NUMERIC
if (dtype:=array.dtype.type) notinvalid_dtypes:
msg=f"Unsupported numpy data type '{dtype}'."
raiseGMTInvalidInput(msg)
returnself[DTYPES[dtype]]
These lines were most recently changed in #3563, but I don't think the bug was caused by that PR, probably somewhere else when we were doing the refactoring. Using the pandas.DatetimeIndex workaround above would correctly turn the array into a np.datetime64 type. The warning about Unable to parse 5 longitude strings is indicating that strings like '2024-12-01 00:00:00', '2024-12-15 00:00:00', ... (note the space in between the date and time) are being passed into pygmt's clib functions.
@seisman, should we revise the logic in that pygmt.clib.Session._check_dtype_and_dim method, or do you think it should be done elsewhere?
# Check if a np.object_ array can be converted to np.str_.
ifarray.dtype==np.object_:
withcontextlib.suppress(TypeError, ValueError):
returnnp.ascontiguousarray(array, dtype=np.str_)
The fix should be easy and should be done in the _to_numpy function.
Actually, I already had a branch to_numpy/datetime(commit 949b124) which can fix the "bug" (didn't realize the existence of the bug when I created the branch).
I can submit a PR for this branch, but I feel it would be better to have PRs #3670, #3687 merged first to ensure that the to_numpy/datetime branch has no side-effects.
Activity
weiji14 commentedon Jan 8, 2025
Hmm yes, I can reproduce this on PyGMT v0.14.0. We'll need to check what changes caused this regression. In the meantime, could you put your list of
pandas.Timestampobjects into apandas.DatetimeIndex(following https://www.pygmt.org/v0.14.0/tutorials/advanced/date_time_charts.html#using-pandas-date-range) like so:produces
yvonnefroehlich commentedon Jan 8, 2025
@eelcodoornbos Thanks for reporting this!
Just checked our Tutorial Plotting datetime charts. There we also have this warning when using
datetime.datetime(not fordatetime.date) in v0.14.0 (please see https://www.pygmt.org/v0.14.0/tutorials/advanced/date_time_charts.html#using-python-s-datetime the second example).I do not remember I saw this warning, when making two minor changes to this tutorial (https://github.com/GenericMappingTools/pygmt/pull/3664/files, December 2).
weiji14 commentedon Jan 8, 2025
So the
xdataarray get converted into a numpy array of typenp.str_instead of annp.datetime64dtype, and is parsed incorrectly asGMT_TEXTinstead ofGMT_DATETIMEhere:pygmt/pygmt/clib/session.py
Lines 942 to 947 in 2a84b5e
These lines were most recently changed in #3563, but I don't think the bug was caused by that PR, probably somewhere else when we were doing the refactoring. Using the
pandas.DatetimeIndexworkaround above would correctly turn the array into anp.datetime64type. The warning aboutUnable to parse 5 longitude stringsis indicating that strings like'2024-12-01 00:00:00', '2024-12-15 00:00:00', ...(note the space in between the date and time) are being passed into pygmt's clib functions.@seisman, should we revise the logic in that
pygmt.clib.Session._check_dtype_and_dimmethod, or do you think it should be done elsewhere?seisman commentedon Jan 9, 2025
xdatais a list of Timestamp objects, and it can't be converted to the numpy.datetime64 dtype by thenp.ascontiguousarrayfunction:Then, it's converted to
np.str_by the following lines:pygmt/pygmt/clib/conversion.py
Lines 197 to 200 in c7198e6
The fix should be easy and should be done in the
_to_numpyfunction.Actually, I already had a branch
to_numpy/datetime(commit 949b124) which can fix the "bug" (didn't realize the existence of the bug when I created the branch).I can submit a PR for this branch, but I feel it would be better to have PRs #3670, #3687 merged first to ensure that the
to_numpy/datetimebranch has no side-effects.