From fb21e91072872cfddff6978520abb9dc5fe29d50 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 17 Jan 2025 12:12:21 +0800 Subject: [PATCH] Fix the bug of converting Python sequence of datetime-like objects (#3760) (#3773) 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):