-
Notifications
You must be signed in to change notification settings - Fork 235
Support timedelta64 dtype as input #2884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4730961
d53138c
51bc55a
0c20c72
49dbe83
d5bedc2
44e62d6
3aa5e3a
849feda
4c3b8e9
4426d95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| arguments, insert common text into docstrings, transform arguments to strings, | ||
| etc. | ||
| """ | ||
| import datetime | ||
| import functools | ||
| import textwrap | ||
| import warnings | ||
|
|
@@ -673,14 +674,19 @@ def kwargs_to_strings(**conversions): | |
| >>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6)) | ||
| {'A': False, 'bla': (1, 2, 3), 'foo': True, 'i': '5,6'} | ||
| args: 123 | ||
|
|
||
| >>> # Test that region accepts arguments with datetime or timedelta type | ||
| >>> import datetime | ||
| >>> module( | ||
| ... R=[ | ||
| ... np.datetime64("2010-01-01T16:00:00"), | ||
| ... datetime.datetime(2020, 1, 1, 12, 23, 45), | ||
| ... np.timedelta64(0, "h"), | ||
| ... np.timedelta64(24, "h"), | ||
| ... ] | ||
| ... ) | ||
| {'R': '2010-01-01T16:00:00/2020-01-01T12:23:45.000000'} | ||
| {'R': '2010-01-01T16:00:00/2020-01-01T12:23:45.000000/0/24'} | ||
|
|
||
| >>> import pandas as pd | ||
| >>> import xarray as xr | ||
| >>> module( | ||
|
|
@@ -690,6 +696,7 @@ def kwargs_to_strings(**conversions): | |
| ... ] | ||
| ... ) | ||
| {'R': '2005-01-01T08:00:00.000000000/2015-01-01T12:00:00.123456'} | ||
|
|
||
| >>> # Here is a more realistic example | ||
| >>> # See https://github.com/GenericMappingTools/pygmt/issues/2361 | ||
| >>> @kwargs_to_strings( | ||
|
|
@@ -760,14 +767,23 @@ def new_module(*args, **kwargs): | |
| if fmt in separators and is_nonstr_iter(value): | ||
| for index, item in enumerate(value): | ||
| if " " in str(item): | ||
| # Check if there is a space " " when converting | ||
| # a pandas.Timestamp/xr.DataArray to a string. | ||
| # If so, use np.datetime_as_string instead. | ||
| # Convert datetime-like item to ISO 8601 | ||
| # string format like YYYY-MM-DDThh:mm:ss.ffffff. | ||
| value[index] = np.datetime_as_string( | ||
| np.asarray(item, dtype=np.datetime64) | ||
| ) | ||
| # Check if there is a space " " in the item, which | ||
| # is typically present in objects such as | ||
| # np.timedelta64, pd.Timestamp, or xr.DataArray. | ||
| # If so, convert the item to a numerical or string | ||
| # type that is understood by GMT as follows: | ||
| if getattr( | ||
| getattr(item, "dtype", ""), "name", "" | ||
| ).startswith("timedelta"): | ||
| # A np.timedelta64 item is cast to integer | ||
| value[index] = item.astype("int") | ||
|
||
| else: | ||
| # A pandas.Timestamp/xr.DataArray containing | ||
| # a datetime-like object is cast to ISO 8601 | ||
| # string format like YYYY-MM-DDThh:mm:ss.ffffff | ||
| value[index] = np.datetime_as_string( | ||
| np.asarray(item, dtype=np.datetime64) | ||
| ) | ||
|
||
| newvalue = separators[fmt].join(f"{item}" for item in value) | ||
| # Changes in bound.arguments will reflect in bound.args | ||
| # and bound.kwargs. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| outs: | ||
| - md5: a045e84c478a7ebc5a62c5b39b8229a5 | ||
| size: 13016 | ||
| path: test_plot_timedelta64.png |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently Python has a built-in
datetime.timedeltaobjects, but it's super hard to cast it to an integer type while preserving the orignal unit (e.g. day, hour, minute, etc). E.g.Should we still support
datetime.timedelta? Or onlynp.timedelta64?