diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index d70be62f076..ea5b3e0eff2 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -85,6 +85,7 @@ np.float64: "GMT_DOUBLE", np.str_: "GMT_TEXT", np.datetime64: "GMT_DATETIME", + np.timedelta64: "GMT_LONG", } # Load the GMT library outside the Session class to avoid repeated loading. diff --git a/pygmt/tests/baseline/test_plot_timedelta64.png.dvc b/pygmt/tests/baseline/test_plot_timedelta64.png.dvc new file mode 100644 index 00000000000..087a903145f --- /dev/null +++ b/pygmt/tests/baseline/test_plot_timedelta64.png.dvc @@ -0,0 +1,5 @@ +outs: +- md5: 8edddcec764d244053c4d675e98732b9 + size: 13201 + path: test_plot_timedelta64.png + hash: md5 diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index bbaf0aced65..b736b2bd15d 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -168,6 +168,37 @@ def test_put_vector_string_dtype(): npt.assert_array_equal(output["y"], expected_vectors[j]) +def test_put_vector_timedelta64_dtype(): + """ + Passing timedelta64 type vectors with various time units (year, month, + week, day, hour, minute, second, millisecond, microsecond) to a dataset. + """ + for unit in ["Y", "M", "W", "D", "h", "m", "s", "ms", "μs"]: + with clib.Session() as lib, GMTTempFile() as tmp_file: + dataset = lib.create_data( + family="GMT_IS_DATASET|GMT_VIA_VECTOR", + geometry="GMT_IS_POINT", + mode="GMT_CONTAINER_ONLY", + dim=[1, 5, 1, 0], # columns, rows, layers, dtype + ) + timedata = np.arange(np.timedelta64(0, unit), np.timedelta64(5, unit)) + lib.put_vector(dataset, column=0, vector=timedata) + # Turns out wesn doesn't matter for Datasets + wesn = [0] * 6 + # Save the data to a file to see if it's being accessed correctly + lib.write_data( + family="GMT_IS_VECTOR", + geometry="GMT_IS_POINT", + mode="GMT_WRITE_SET", + wesn=wesn, + output=tmp_file.name, + data=dataset, + ) + # Load the data and check that it's correct + newtimedata = tmp_file.loadtxt(unpack=True, dtype=f"timedelta64[{unit}]") + npt.assert_equal(actual=newtimedata, desired=timedata) + + def test_put_vector_invalid_dtype(): """ Check that it fails with an exception for invalid data types. diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index a072b1461d5..be6d25161cc 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -453,6 +453,26 @@ def test_plot_datetime(): return fig +@pytest.mark.mpl_image_compare +def test_plot_timedelta64(): + """ + Test plotting numpy.timedelta64 input data. + """ + fig = Figure() + fig.basemap( + projection="X8c/5c", + region=[0, 8, 0, 10], + frame=["WSne", "xaf+lForecast Days", "yaf+lRMSE"], + ) + fig.plot( + x=np.arange(np.timedelta64(0, "D"), np.timedelta64(8, "D")), + y=np.geomspace(start=0.1, stop=9, num=8), + style="c0.2c", + pen="1p", + ) + return fig + + @pytest.mark.mpl_image_compare( filename="test_plot_ogrgmt_file_multipoint_default_style.png" )