From 4d0b45f0d0ba1bb3756154047dc9228b509f3d0f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 12 Feb 2025 22:39:53 +0800 Subject: [PATCH 1/3] Add a regression text for passing strings with numeric values --- .../baseline/test_text_numeric_text.png.dvc | 5 +++++ pygmt/tests/test_text.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 pygmt/tests/baseline/test_text_numeric_text.png.dvc diff --git a/pygmt/tests/baseline/test_text_numeric_text.png.dvc b/pygmt/tests/baseline/test_text_numeric_text.png.dvc new file mode 100644 index 00000000000..983ed9b68fe --- /dev/null +++ b/pygmt/tests/baseline/test_text_numeric_text.png.dvc @@ -0,0 +1,5 @@ +outs: +- md5: 97d7780216d535c0400f6117cf47ff65 + size: 8657 + hash: md5 + path: test_text_numeric_text.png diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 22b446302b8..1da1ac01bf0 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -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): From eb310c83fe2680807de2eb51d8fee3683adc709e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 12 Feb 2025 22:49:32 +0800 Subject: [PATCH 2/3] Do not try to convert string array to np.datetime64 dtype --- pygmt/clib/conversion.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 4a52afb497b..c23ea22b9d9 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -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 From 6f825cd9a39c8ccb138e1e7786268d174729f8f4 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 12 Feb 2025 23:00:55 +0800 Subject: [PATCH 3/3] Fix the _to_numpy test for datetime strings --- pygmt/tests/test_clib_to_numpy.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_clib_to_numpy.py b/pygmt/tests/test_clib_to_numpy.py index 40b45e466d8..95bfcb3aaa8 100644 --- a/pygmt/tests/test_clib_to_numpy.py +++ b/pygmt/tests/test_clib_to_numpy.py @@ -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), @@ -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. #