Skip to content

Commit 3661e54

Browse files
committed
Refactor vectors_to_arrays and deprecate the array_to_datetime function
1 parent c2e429c commit 3661e54

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

pygmt/clib/conversion.py

+12
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ def vectors_to_arrays(vectors: Sequence[Any]) -> list[np.ndarray]:
224224
else:
225225
vec_dtype = str(getattr(vector, "dtype", ""))
226226
array = np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype))
227+
# Convert np.object_ to np.datetime64 or np.str_.
228+
# If fails, then the array can't be recognized.
229+
if array.dtype.type == np.object_:
230+
try:
231+
array = np.asarray(array, dtype=np.datetime64)
232+
except ValueError:
233+
array = np.asarray(array, dtype=np.str_)
227234
arrays.append(array)
228235
return arrays
229236

@@ -313,6 +320,11 @@ def array_to_datetime(array: Sequence[Any]) -> np.ndarray:
313320
314321
If the input array is not in legal datetime formats, raise a ValueError exception.
315322
323+
.. deprecated:: 0.14.0
324+
325+
The function is no longer used in the PyGMT project, but we keep this function
326+
to document the supported datetime types.
327+
316328
Parameters
317329
----------
318330
array

pygmt/clib/session.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pandas as pd
1919
import xarray as xr
2020
from pygmt.clib.conversion import (
21-
array_to_datetime,
2221
dataarray_to_matrix,
2322
sequence_to_ctypes_array,
2423
strings_to_ctypes_array,
@@ -854,22 +853,13 @@ def _check_dtype_and_dim(self, array, ndim):
854853
"""
855854
# Check that the array has the given number of dimensions
856855
if array.ndim != ndim:
857-
raise GMTInvalidInput(
858-
f"Expected a numpy {ndim}-D array, got {array.ndim}-D."
859-
)
856+
msg = f"Expected a numpy {ndim}-D array, got {array.ndim}-D."
857+
raise GMTInvalidInput(msg)
860858

861859
# Check that the array has a valid/known data type
862860
if array.dtype.type not in DTYPES:
863-
try:
864-
if array.dtype.type is np.object_:
865-
# Try to convert unknown object type to np.datetime64
866-
array = array_to_datetime(array)
867-
else:
868-
raise ValueError
869-
except ValueError as e:
870-
raise GMTInvalidInput(
871-
f"Unsupported numpy data type '{array.dtype.type}'."
872-
) from e
861+
msg = f"Unsupported numpy data type '{array.dtype.type}'."
862+
raise GMTInvalidInput(msg)
873863
return self[DTYPES[array.dtype.type]]
874864

875865
def put_vector(self, dataset, column, vector):
@@ -917,7 +907,7 @@ def put_vector(self, dataset, column, vector):
917907
gmt_type = self._check_dtype_and_dim(vector, ndim=1)
918908
if gmt_type in {self["GMT_TEXT"], self["GMT_DATETIME"]}:
919909
if gmt_type == self["GMT_DATETIME"]:
920-
vector = np.datetime_as_string(array_to_datetime(vector))
910+
vector = np.datetime_as_string(vector)
921911
vector_pointer = strings_to_ctypes_array(vector)
922912
else:
923913
vector_pointer = vector.ctypes.data_as(ctp.c_void_p)

0 commit comments

Comments
 (0)