Skip to content

Commit

Permalink
Refactor vectors_to_arrays and deprecate the array_to_datetime function
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Oct 13, 2024
1 parent c2e429c commit 3661e54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
12 changes: 12 additions & 0 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
20 changes: 5 additions & 15 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3661e54

Please sign in to comment.