5
5
arguments, insert common text into docstrings, transform arguments to strings,
6
6
etc.
7
7
"""
8
+ import datetime
8
9
import functools
9
10
import textwrap
10
11
import warnings
@@ -673,14 +674,19 @@ def kwargs_to_strings(**conversions):
673
674
>>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6))
674
675
{'A': False, 'bla': (1, 2, 3), 'foo': True, 'i': '5,6'}
675
676
args: 123
677
+
678
+ >>> # Test that region accepts arguments with datetime or timedelta type
676
679
>>> import datetime
677
680
>>> module(
678
681
... R=[
679
682
... np.datetime64("2010-01-01T16:00:00"),
680
683
... datetime.datetime(2020, 1, 1, 12, 23, 45),
684
+ ... np.timedelta64(0, "h"),
685
+ ... np.timedelta64(24, "h"),
681
686
... ]
682
687
... )
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
+
684
690
>>> import pandas as pd
685
691
>>> import xarray as xr
686
692
>>> module(
@@ -690,6 +696,7 @@ def kwargs_to_strings(**conversions):
690
696
... ]
691
697
... )
692
698
{'R': '2005-01-01T08:00:00.000000000/2015-01-01T12:00:00.123456'}
699
+
693
700
>>> # Here is a more realistic example
694
701
>>> # See https://github.com/GenericMappingTools/pygmt/issues/2361
695
702
>>> @kwargs_to_strings(
@@ -760,14 +767,23 @@ def new_module(*args, **kwargs):
760
767
if fmt in separators and is_nonstr_iter (value ):
761
768
for index , item in enumerate (value ):
762
769
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
+ )
771
787
newvalue = separators [fmt ].join (f"{ item } " for item in value )
772
788
# Changes in bound.arguments will reflect in bound.args
773
789
# and bound.kwargs.
0 commit comments