From 3661e548c55cec10acbfb48eb5644ee8c03d0906 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 21:37:25 +0800 Subject: [PATCH] Refactor vectors_to_arrays and deprecate the array_to_datetime function --- pygmt/clib/conversion.py | 12 ++++++++++++ pygmt/clib/session.py | 20 +++++--------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 07f8756adcd..1db93768f51 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -224,6 +224,13 @@ def vectors_to_arrays(vectors: Sequence[Any]) -> list[np.ndarray]: else: vec_dtype = str(getattr(vector, "dtype", "")) array = np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype)) + # Convert np.object_ to np.datetime64 or np.str_. + # If fails, then the array can't be recognized. + if array.dtype.type == np.object_: + try: + array = np.asarray(array, dtype=np.datetime64) + except ValueError: + array = np.asarray(array, dtype=np.str_) arrays.append(array) return arrays @@ -313,6 +320,11 @@ def array_to_datetime(array: Sequence[Any]) -> np.ndarray: If the input array is not in legal datetime formats, raise a ValueError exception. + .. deprecated:: 0.14.0 + + The function is no longer used in the PyGMT project, but we keep this function + to document the supported datetime types. + Parameters ---------- array diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 903edfc12c7..eea6e0f6efc 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -18,7 +18,6 @@ import pandas as pd import xarray as xr from pygmt.clib.conversion import ( - array_to_datetime, dataarray_to_matrix, sequence_to_ctypes_array, strings_to_ctypes_array, @@ -854,22 +853,13 @@ def _check_dtype_and_dim(self, array, ndim): """ # Check that the array has the given number of dimensions if array.ndim != ndim: - raise GMTInvalidInput( - f"Expected a numpy {ndim}-D array, got {array.ndim}-D." - ) + msg = f"Expected a numpy {ndim}-D array, got {array.ndim}-D." + raise GMTInvalidInput(msg) # Check that the array has a valid/known data type if array.dtype.type not in DTYPES: - try: - if array.dtype.type is np.object_: - # Try to convert unknown object type to np.datetime64 - array = array_to_datetime(array) - else: - raise ValueError - except ValueError as e: - raise GMTInvalidInput( - f"Unsupported numpy data type '{array.dtype.type}'." - ) from e + msg = f"Unsupported numpy data type '{array.dtype.type}'." + raise GMTInvalidInput(msg) return self[DTYPES[array.dtype.type]] def put_vector(self, dataset, column, vector): @@ -917,7 +907,7 @@ def put_vector(self, dataset, column, vector): gmt_type = self._check_dtype_and_dim(vector, ndim=1) if gmt_type in {self["GMT_TEXT"], self["GMT_DATETIME"]}: if gmt_type == self["GMT_DATETIME"]: - vector = np.datetime_as_string(array_to_datetime(vector)) + vector = np.datetime_as_string(vector) vector_pointer = strings_to_ctypes_array(vector) else: vector_pointer = vector.ctypes.data_as(ctp.c_void_p)