Skip to content

Commit d5dd5ac

Browse files
authored
Support timedelta64 dtype as input (#2884)
Map np.timedelta64 to GMT_LONG in clib/session.py's DTYPES dictionary. Added a unit test in test_clib_put_vectors.py to test passing a numpy array with timedelta64 dtypes of various time units (year to microsecond). * Add unit test for plotting timedelta64 data Make a 2D plot with Forecast Days (timedelta64) on the x-axis, and RMSE on the y-axis. * Update test_plot_timedelta64 baseline img with GMT 6.5.0 and gs 10.02.1
1 parent c9c3a4a commit d5dd5ac

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

pygmt/clib/session.py

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
np.float64: "GMT_DOUBLE",
8686
np.str_: "GMT_TEXT",
8787
np.datetime64: "GMT_DATETIME",
88+
np.timedelta64: "GMT_LONG",
8889
}
8990

9091
# Load the GMT library outside the Session class to avoid repeated loading.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: 8edddcec764d244053c4d675e98732b9
3+
size: 13201
4+
path: test_plot_timedelta64.png
5+
hash: md5

pygmt/tests/test_clib_put_vector.py

+31
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,37 @@ def test_put_vector_string_dtype():
168168
npt.assert_array_equal(output["y"], expected_vectors[j])
169169

170170

171+
def test_put_vector_timedelta64_dtype():
172+
"""
173+
Passing timedelta64 type vectors with various time units (year, month,
174+
week, day, hour, minute, second, millisecond, microsecond) to a dataset.
175+
"""
176+
for unit in ["Y", "M", "W", "D", "h", "m", "s", "ms", "μs"]:
177+
with clib.Session() as lib, GMTTempFile() as tmp_file:
178+
dataset = lib.create_data(
179+
family="GMT_IS_DATASET|GMT_VIA_VECTOR",
180+
geometry="GMT_IS_POINT",
181+
mode="GMT_CONTAINER_ONLY",
182+
dim=[1, 5, 1, 0], # columns, rows, layers, dtype
183+
)
184+
timedata = np.arange(np.timedelta64(0, unit), np.timedelta64(5, unit))
185+
lib.put_vector(dataset, column=0, vector=timedata)
186+
# Turns out wesn doesn't matter for Datasets
187+
wesn = [0] * 6
188+
# Save the data to a file to see if it's being accessed correctly
189+
lib.write_data(
190+
family="GMT_IS_VECTOR",
191+
geometry="GMT_IS_POINT",
192+
mode="GMT_WRITE_SET",
193+
wesn=wesn,
194+
output=tmp_file.name,
195+
data=dataset,
196+
)
197+
# Load the data and check that it's correct
198+
newtimedata = tmp_file.loadtxt(unpack=True, dtype=f"timedelta64[{unit}]")
199+
npt.assert_equal(actual=newtimedata, desired=timedata)
200+
201+
171202
def test_put_vector_invalid_dtype():
172203
"""
173204
Check that it fails with an exception for invalid data types.

pygmt/tests/test_plot.py

+20
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,26 @@ def test_plot_datetime():
453453
return fig
454454

455455

456+
@pytest.mark.mpl_image_compare
457+
def test_plot_timedelta64():
458+
"""
459+
Test plotting numpy.timedelta64 input data.
460+
"""
461+
fig = Figure()
462+
fig.basemap(
463+
projection="X8c/5c",
464+
region=[0, 8, 0, 10],
465+
frame=["WSne", "xaf+lForecast Days", "yaf+lRMSE"],
466+
)
467+
fig.plot(
468+
x=np.arange(np.timedelta64(0, "D"), np.timedelta64(8, "D")),
469+
y=np.geomspace(start=0.1, stop=9, num=8),
470+
style="c0.2c",
471+
pen="1p",
472+
)
473+
return fig
474+
475+
456476
@pytest.mark.mpl_image_compare(
457477
filename="test_plot_ogrgmt_file_multipoint_default_style.png"
458478
)

0 commit comments

Comments
 (0)