Skip to content

Commit a4a1676

Browse files
authored
Fix the bug for passing text strings with numeric values (#3804)
* Add a regression text for passing strings with numeric values * Do not try to convert string array to np.datetime64 dtype * Fix the _to_numpy test for datetime strings
1 parent c80923c commit a4a1676

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

pygmt/clib/conversion.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,13 @@ 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-
213-
# Check if a np.object_ array can be converted to np.str_.
214-
if array.dtype == np.object_:
215-
with contextlib.suppress(TypeError, ValueError):
216-
return np.ascontiguousarray(array, dtype=np.str_)
208+
# Check if a np.object_ array can be converted to np.datetime64 or np.str_.
209+
# Try np.datetime64 first then np.str_, because datetime-like objects usually have
210+
# string representations.
211+
if array.dtype.type == np.object_:
212+
for dtype in [np.datetime64, np.str_]:
213+
with contextlib.suppress(TypeError, ValueError):
214+
return np.ascontiguousarray(array, dtype=dtype)
217215
return array
218216

219217

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: 97d7780216d535c0400f6117cf47ff65
3+
size: 8657
4+
hash: md5
5+
path: test_text_numeric_text.png

pygmt/tests/test_clib_to_numpy.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ def test_to_numpy_python_types(data, expected_dtype):
8383
@pytest.mark.parametrize(
8484
"data",
8585
[
86-
pytest.param(
87-
["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"], id="iso8601"
88-
),
8986
pytest.param(
9087
[
9188
datetime.date(2018, 1, 1),
@@ -144,6 +141,20 @@ def test_to_numpy_python_datetime(data):
144141
)
145142

146143

144+
def test_to_numpy_python_datetime_string():
145+
"""
146+
Test the _to_numpy function with Python sequence of datetime strings.
147+
148+
Datetime strings are NOT converted to np.datetime64, but GMT can be process the
149+
strings as datetime strings.
150+
"""
151+
result = _to_numpy(["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"])
152+
_check_result(result, np.str_)
153+
npt.assert_array_equal(
154+
result, ["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"]
155+
)
156+
157+
147158
########################################################################################
148159
# Test the _to_numpy function with NumPy arrays.
149160
#

pygmt/tests/test_text.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,25 @@ def test_text_nonstr_text():
426426
return fig
427427

428428

429+
@pytest.mark.mpl_image_compare
430+
def test_text_numeric_text():
431+
"""
432+
Test passing text strings that are numeric.
433+
434+
Regression test for https://github.com/GenericMappingTools/pygmt/issues/3803.
435+
"""
436+
fig = Figure()
437+
fig.text(
438+
region=[0, 10, 0, 5],
439+
projection="X10c/5c",
440+
frame=True,
441+
x=[1, 2, 3, 4],
442+
y=[1, 2, 3, 4],
443+
text=["2012", "2013", "2014", "2015"],
444+
)
445+
return fig
446+
447+
429448
@pytest.mark.mpl_image_compare(filename="test_text_nonascii.png")
430449
@pytest.mark.parametrize("encoding", ["ISOLatin1+", "Standard+"])
431450
def test_text_nonascii(encoding):

0 commit comments

Comments
 (0)