From 0bba51e0b47e9806a823e56a9ca45f22f1327f85 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Jan 2025 08:11:09 +0800 Subject: [PATCH] Fix the bug of converting Python sequence of datetime-like objects (#3760) Convert unrecognized objects to datetime before converting to string dtype --- pygmt/clib/conversion.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index c54923705dc..2f9df346bd9 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -194,6 +194,11 @@ 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):