Skip to content

Commit 4939ee2

Browse files
authored
Allow datetime inputs to region argument (#562)
Includes support for: - Python datetime objects - np.datetime64 - pandas.Timestamp - xarray.DataArray with datetime64 dtype
1 parent 2ddbb0e commit 4939ee2

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

pygmt/helpers/decorators.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import textwrap
99
import functools
1010

11+
import numpy as np
12+
1113
from .utils import is_nonstr_iter
1214
from ..exceptions import GMTInvalidInput
1315

@@ -302,6 +304,23 @@ def kwargs_to_strings(convert_bools=True, **conversions):
302304
>>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6))
303305
{'bla': (1, 2, 3), 'foo': '', 'i': '5,6'}
304306
args: 123
307+
>>> import datetime
308+
>>> module(
309+
... R=[
310+
... np.datetime64("2010-01-01T16:00:00"),
311+
... datetime.datetime(2020, 1, 1, 12, 23, 45),
312+
... ]
313+
... )
314+
{'R': '2010-01-01T16:00:00/2020-01-01T12:23:45.000000'}
315+
>>> import pandas as pd
316+
>>> import xarray as xr
317+
>>> module(
318+
... R=[
319+
... xr.DataArray(data=np.datetime64("2005-01-01T08:00:00")),
320+
... pd.Timestamp("2015-01-01T12:00:00.123456789"),
321+
... ]
322+
... )
323+
{'R': '2005-01-01T08:00:00.000000000/2015-01-01T12:00:00.123456'}
305324
306325
"""
307326
valid_conversions = [
@@ -338,9 +357,19 @@ def new_module(*args, **kwargs):
338357
value = kwargs[arg]
339358
issequence = fmt in separators
340359
if issequence and is_nonstr_iter(value):
341-
kwargs[arg] = separators[fmt].join(
342-
"{}".format(item) for item in value
343-
)
360+
for index, item in enumerate(value):
361+
try:
362+
# check if there is a space " " when converting
363+
# a pandas.Timestamp/xr.DataArray to a string.
364+
# If so, use np.datetime_as_string instead.
365+
assert " " not in str(item)
366+
except AssertionError:
367+
# convert datetime-like item to ISO 8601
368+
# string format like YYYY-MM-DDThh:mm:ss.ffffff
369+
value[index] = np.datetime_as_string(
370+
np.asarray(item, dtype=np.datetime64)
371+
)
372+
kwargs[arg] = separators[fmt].join(f"{item}" for item in value)
344373
# Execute the original function and return its output
345374
return module_func(*args, **kwargs)
346375

pygmt/tests/test_plot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,16 @@ def test_plot_scalar_xy():
294294
def test_plot_datetime():
295295
"""Test various datetime input data"""
296296
fig = Figure()
297-
fig.basemap(projection="X15c/5c", region="2010-01-01/2020-01-01/0/10", frame=True)
297+
fig.basemap(
298+
projection="X15c/5c",
299+
region=[
300+
np.array("2010-01-01T00:00:00", dtype=np.datetime64),
301+
pd.Timestamp("2020-01-01"),
302+
0,
303+
10,
304+
],
305+
frame=True,
306+
)
298307

299308
# numpy.datetime64 types
300309
x = np.array(

0 commit comments

Comments
 (0)