Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,13 @@ def _to_numpy(data: Any) -> np.ndarray:

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

# Check if a np.object_ or np.str_ array can be converted to np.datetime64.
if array.dtype.type in {np.object_, np.str_}:
with contextlib.suppress(TypeError, ValueError):
return np.ascontiguousarray(array, dtype=np.datetime64)

# Check if a np.object_ array can be converted to np.str_.
if array.dtype == np.object_:
with contextlib.suppress(TypeError, ValueError):
return np.ascontiguousarray(array, dtype=np.str_)
# Check if a np.object_ array can be converted to np.datetime64 or np.str_.
# Try np.datetime64 first then np.str_, because datetime-like objects usually have
# string representations.
if array.dtype.type == np.object_:
for dtype in [np.datetime64, np.str_]:
with contextlib.suppress(TypeError, ValueError):
return np.ascontiguousarray(array, dtype=dtype)
return array


Expand Down
5 changes: 5 additions & 0 deletions pygmt/tests/baseline/test_text_numeric_text.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
outs:
- md5: 97d7780216d535c0400f6117cf47ff65
size: 8657
hash: md5
path: test_text_numeric_text.png
17 changes: 14 additions & 3 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ def test_to_numpy_python_types(data, expected_dtype):
@pytest.mark.parametrize(
"data",
[
pytest.param(
["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"], id="iso8601"
),
pytest.param(
[
datetime.date(2018, 1, 1),
Expand Down Expand Up @@ -144,6 +141,20 @@ def test_to_numpy_python_datetime(data):
)


def test_to_numpy_python_datetime_string():
"""
Test the _to_numpy function with Python sequence of datetime strings.

Datetime strings are NOT converted to np.datetime64, but GMT can be process the
strings as datetime strings.
"""
result = _to_numpy(["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"])
_check_result(result, np.str_)
npt.assert_array_equal(
result, ["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"]
)


########################################################################################
# Test the _to_numpy function with NumPy arrays.
#
Expand Down
19 changes: 19 additions & 0 deletions pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,25 @@ def test_text_nonstr_text():
return fig


@pytest.mark.mpl_image_compare
def test_text_numeric_text():
"""
Test passing text strings that are numeric.

Regression test for https://github.com/GenericMappingTools/pygmt/issues/3803.
"""
fig = Figure()
fig.text(
region=[0, 10, 0, 5],
projection="X10c/5c",
frame=True,
x=[1, 2, 3, 4],
y=[1, 2, 3, 4],
text=["2012", "2013", "2014", "2015"],
)
return fig


@pytest.mark.mpl_image_compare(filename="test_text_nonascii.png")
@pytest.mark.parametrize("encoding", ["ISOLatin1+", "Standard+"])
def test_text_nonascii(encoding):
Expand Down