Skip to content

Commit 190e885

Browse files
authored
Merge branch 'main' into gallery-hlines-vlines
2 parents 8b020ce + 900e8d4 commit 190e885

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

Diff for: pygmt/clib/conversion.py

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ def _to_numpy(data: Any) -> np.ndarray:
205205

206206
array = np.ascontiguousarray(data, dtype=numpy_dtype)
207207

208+
# Check if a np.object_ or np.str_ array can be converted to np.datetime64.
209+
if array.dtype.type in {np.object_, np.str_}:
210+
with contextlib.suppress(TypeError, ValueError):
211+
return np.ascontiguousarray(array, dtype=np.datetime64)
212+
208213
# Check if a np.object_ array can be converted to np.str_.
209214
if array.dtype == np.object_:
210215
with contextlib.suppress(TypeError, ValueError):

Diff for: pygmt/tests/baseline/test_plot_datetime.png.dvc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
outs:
2-
- md5: 583947facaa873122f0bf18137809cd4
3-
size: 12695
2+
- md5: 0a2eae0da1e3d5b71d7392de1c081346
3+
size: 13124
44
path: test_plot_datetime.png
55
hash: md5

Diff for: pygmt/tests/test_clib_to_numpy.py

+72-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Tests for the _to_numpy function in the clib.conversion module.
33
"""
44

5+
import datetime
56
import sys
6-
from datetime import date, datetime
77

88
import numpy as np
99
import numpy.testing as npt
@@ -80,6 +80,70 @@ def test_to_numpy_python_types(data, expected_dtype):
8080
npt.assert_array_equal(result, data)
8181

8282

83+
@pytest.mark.parametrize(
84+
"data",
85+
[
86+
pytest.param(
87+
["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"], id="iso8601"
88+
),
89+
pytest.param(
90+
[
91+
datetime.date(2018, 1, 1),
92+
datetime.datetime(2018, 2, 1),
93+
datetime.date(2018, 3, 1),
94+
datetime.datetime(2018, 4, 1, 1, 2, 3),
95+
],
96+
id="datetime",
97+
),
98+
pytest.param(
99+
[
100+
np.datetime64("2018"),
101+
np.datetime64("2018-02"),
102+
np.datetime64("2018-03-01"),
103+
np.datetime64("2018-04-01T01:02:03"),
104+
],
105+
id="np_datetime64",
106+
),
107+
pytest.param(
108+
[
109+
pd.Timestamp("2018-01-01"),
110+
pd.Timestamp("2018-02-01"),
111+
pd.Timestamp("2018-03-01"),
112+
pd.Timestamp("2018-04-01T01:02:03"),
113+
],
114+
id="pd_timestamp",
115+
),
116+
pytest.param(
117+
[
118+
"2018-01-01",
119+
np.datetime64("2018-02-01"),
120+
datetime.datetime(2018, 3, 1),
121+
pd.Timestamp("2018-04-01T01:02:03"),
122+
],
123+
id="mixed",
124+
),
125+
],
126+
)
127+
def test_to_numpy_python_datetime(data):
128+
"""
129+
Test the _to_numpy function with Python sequence of datetime types.
130+
"""
131+
result = _to_numpy(data)
132+
assert result.dtype.type == np.datetime64
133+
npt.assert_array_equal(
134+
result,
135+
np.array(
136+
[
137+
"2018-01-01T00:00:00",
138+
"2018-02-01T00:00:00",
139+
"2018-03-01T00:00:00",
140+
"2018-04-01T01:02:03",
141+
],
142+
dtype="datetime64[s]",
143+
),
144+
)
145+
146+
83147
########################################################################################
84148
# Test the _to_numpy function with NumPy arrays.
85149
#
@@ -603,9 +667,9 @@ def test_to_numpy_pyarrow_date(dtype, expected_dtype):
603667
Here we explicitly check the dtype and date unit of the result.
604668
"""
605669
data = [
606-
date(2024, 1, 1),
607-
datetime(2024, 1, 2),
608-
datetime(2024, 1, 3),
670+
datetime.date(2024, 1, 1),
671+
datetime.datetime(2024, 1, 2),
672+
datetime.datetime(2024, 1, 3),
609673
]
610674
array = pa.array(data, type=dtype)
611675
result = _to_numpy(array)
@@ -649,7 +713,10 @@ def test_to_numpy_pyarrow_timestamp(dtype, expected_dtype):
649713
650714
Reference: https://arrow.apache.org/docs/python/generated/pyarrow.timestamp.html
651715
"""
652-
data = [datetime(2024, 1, 2, 3, 4, 5), datetime(2024, 1, 2, 3, 4, 6)]
716+
data = [
717+
datetime.datetime(2024, 1, 2, 3, 4, 5),
718+
datetime.datetime(2024, 1, 2, 3, 4, 6),
719+
]
653720
array = pa.array(data, type=dtype)
654721
result = _to_numpy(array)
655722
_check_result(result, np.datetime64)

Diff for: pygmt/tests/test_plot.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,14 @@ def test_plot_datetime():
467467
fig.plot(x=x, y=y, style="a0.2c", pen="1p")
468468

469469
# the Python built-in datetime and date
470-
x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)]
470+
x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1, 0, 0, 0)]
471471
y = [8.5, 9.5]
472472
fig.plot(x=x, y=y, style="i0.2c", pen="1p")
473+
474+
# Python sequence of pd.Timestamp
475+
x = [pd.Timestamp("2018-01-01"), pd.Timestamp("2019-01-01")]
476+
y = [5.5, 6.5]
477+
fig.plot(x=x, y=y, style="d0.2c", pen="1p")
473478
return fig
474479

475480

0 commit comments

Comments
 (0)