Skip to content

Commit d5bedc2

Browse files
committed
Allow np.timedelta64 inputs to region parameter
Cast np.timedelta64 inputs to int, so that they can be understood by GMT.
1 parent 49dbe83 commit d5bedc2

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

pygmt/helpers/decorators.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
arguments, insert common text into docstrings, transform arguments to strings,
66
etc.
77
"""
8+
import datetime
89
import functools
910
import textwrap
1011
import warnings
@@ -673,14 +674,19 @@ def kwargs_to_strings(**conversions):
673674
>>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6))
674675
{'A': False, 'bla': (1, 2, 3), 'foo': True, 'i': '5,6'}
675676
args: 123
677+
678+
>>> # Test that region accepts arguments with datetime or timedelta type
676679
>>> import datetime
677680
>>> module(
678681
... R=[
679682
... np.datetime64("2010-01-01T16:00:00"),
680683
... datetime.datetime(2020, 1, 1, 12, 23, 45),
684+
... np.timedelta64(0, "h"),
685+
... np.timedelta64(24, "h"),
681686
... ]
682687
... )
683-
{'R': '2010-01-01T16:00:00/2020-01-01T12:23:45.000000'}
688+
{'R': '2010-01-01T16:00:00/2020-01-01T12:23:45.000000/0/24'}
689+
684690
>>> import pandas as pd
685691
>>> import xarray as xr
686692
>>> module(
@@ -690,6 +696,7 @@ def kwargs_to_strings(**conversions):
690696
... ]
691697
... )
692698
{'R': '2005-01-01T08:00:00.000000000/2015-01-01T12:00:00.123456'}
699+
693700
>>> # Here is a more realistic example
694701
>>> # See https://github.com/GenericMappingTools/pygmt/issues/2361
695702
>>> @kwargs_to_strings(
@@ -760,14 +767,23 @@ def new_module(*args, **kwargs):
760767
if fmt in separators and is_nonstr_iter(value):
761768
for index, item in enumerate(value):
762769
if " " in str(item):
763-
# Check if there is a space " " when converting
764-
# a pandas.Timestamp/xr.DataArray to a string.
765-
# If so, use np.datetime_as_string instead.
766-
# Convert datetime-like item to ISO 8601
767-
# string format like YYYY-MM-DDThh:mm:ss.ffffff.
768-
value[index] = np.datetime_as_string(
769-
np.asarray(item, dtype=np.datetime64)
770-
)
770+
# Check if there is a space " " in the item, which
771+
# is typically present in objects such as
772+
# np.timedelta64, pd.Timestamp, or xr.DataArray.
773+
# If so, convert the item to a numerical or string
774+
# type that is understood by GMT as follows:
775+
if getattr(
776+
getattr(item, "dtype", ""), "name", ""
777+
).startswith("timedelta"):
778+
# A np.timedelta64 item is cast to integer
779+
value[index] = item.astype("int")
780+
else:
781+
# A pandas.Timestamp/xr.DataArray containing
782+
# a datetime-like object is cast to ISO 8601
783+
# string format like YYYY-MM-DDThh:mm:ss.ffffff
784+
value[index] = np.datetime_as_string(
785+
np.asarray(item, dtype=np.datetime64)
786+
)
771787
newvalue = separators[fmt].join(f"{item}" for item in value)
772788
# Changes in bound.arguments will reflect in bound.args
773789
# and bound.kwargs.

pygmt/tests/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def test_plot_timedelta64():
460460
fig = Figure()
461461
fig.basemap(
462462
projection="X8c/5c",
463-
region=[0, 8, 0, 10],
463+
region=[np.timedelta64(0, "D"), np.timedelta64(8, "D"), 0, 10],
464464
frame=["WSne", "xaf+lForecast Days", "yaf+lRMSE"],
465465
)
466466
fig.plot(

0 commit comments

Comments
 (0)