diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 7823aa32103..4a52afb497b 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -344,81 +344,3 @@ def strings_to_ctypes_array(strings: Sequence[str] | np.ndarray) -> ctp.Array: ['first', 'second', 'third'] """ return (ctp.c_char_p * len(strings))(*[s.encode() for s in strings]) - - -def array_to_datetime(array: Sequence[Any] | np.ndarray) -> np.ndarray: - """ - Convert a 1-D datetime array from various types into numpy.datetime64. - - If the input array is not in legal datetime formats, raise a ValueError exception. - - Parameters - ---------- - array - The input datetime array in various formats. - - Supported types: - - - str - - numpy.datetime64 - - pandas.DateTimeIndex - - datetime.datetime and datetime.date - - Returns - ------- - array - 1-D datetime array in numpy.datetime64. - - Raises - ------ - ValueError - If the datetime string is invalid. - - Examples - -------- - >>> import datetime - >>> # numpy.datetime64 array - >>> x = np.array( - ... ["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], - ... dtype="datetime64[ns]", - ... ) - >>> array_to_datetime(x) - array(['2010-06-01T00:00:00.000000000', '2011-06-01T12:00:00.000000000', - '2012-01-01T12:34:56.000000000'], dtype='datetime64[ns]') - - >>> # pandas.DateTimeIndex array - >>> import pandas as pd - >>> x = pd.date_range("2013", freq="YS", periods=3) - >>> array_to_datetime(x) - array(['2013-01-01T00:00:00.000000000', '2014-01-01T00:00:00.000000000', - '2015-01-01T00:00:00.000000000'], dtype='datetime64[ns]') - - >>> # Python's built-in date and datetime - >>> x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)] - >>> array_to_datetime(x) - array(['2018-01-01T00:00:00.000000', '2019-01-01T00:00:00.000000'], - dtype='datetime64[us]') - - >>> # Raw datetime strings in various format - >>> x = [ - ... "2018", - ... "2018-02", - ... "2018-03-01", - ... "2018-04-01T01:02:03", - ... ] - >>> array_to_datetime(x) - array(['2018-01-01T00:00:00', '2018-02-01T00:00:00', - '2018-03-01T00:00:00', '2018-04-01T01:02:03'], - dtype='datetime64[s]') - - >>> # Mixed datetime types - >>> x = [ - ... "2018-01-01", - ... np.datetime64("2018-01-01"), - ... datetime.datetime(2018, 1, 1), - ... ] - >>> array_to_datetime(x) - array(['2018-01-01T00:00:00.000000', '2018-01-01T00:00:00.000000', - '2018-01-01T00:00:00.000000'], dtype='datetime64[us]') - """ - return np.asarray(array, dtype=np.datetime64) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 179737d35f9..56f9def2412 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -17,7 +17,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, @@ -934,11 +933,6 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int: msg = f"Expected a numpy {ndim}-D array, got {array.ndim}-D." raise GMTInvalidInput(msg) - # For 1-D arrays, try to convert unknown object type to np.datetime64. - if ndim == 1 and array.dtype.type is np.object_: - with contextlib.suppress(ValueError): - array = array_to_datetime(array) - # 1-D arrays can be numeric or text, 2-D arrays can only be numeric. valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC if (dtype := array.dtype.type) not in valid_dtypes: @@ -993,7 +987,7 @@ def put_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)