|
8 | 8 | import textwrap
|
9 | 9 | import functools
|
10 | 10 |
|
| 11 | +import numpy as np |
| 12 | + |
11 | 13 | from .utils import is_nonstr_iter
|
12 | 14 | from ..exceptions import GMTInvalidInput
|
13 | 15 |
|
@@ -302,6 +304,23 @@ def kwargs_to_strings(convert_bools=True, **conversions):
|
302 | 304 | >>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6))
|
303 | 305 | {'bla': (1, 2, 3), 'foo': '', 'i': '5,6'}
|
304 | 306 | 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'} |
305 | 324 |
|
306 | 325 | """
|
307 | 326 | valid_conversions = [
|
@@ -338,9 +357,19 @@ def new_module(*args, **kwargs):
|
338 | 357 | value = kwargs[arg]
|
339 | 358 | issequence = fmt in separators
|
340 | 359 | 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) |
344 | 373 | # Execute the original function and return its output
|
345 | 374 | return module_func(*args, **kwargs)
|
346 | 375 |
|
|
0 commit comments