Skip to content

Commit 5873138

Browse files
authored
Merge branch 'main' into enums/grid-reg-type
2 parents 626d9da + 048bdc5 commit 5873138

File tree

2 files changed

+1
-85
lines changed

2 files changed

+1
-85
lines changed

pygmt/clib/conversion.py

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -344,81 +344,3 @@ def strings_to_ctypes_array(strings: Sequence[str] | np.ndarray) -> ctp.Array:
344344
['first', 'second', 'third']
345345
"""
346346
return (ctp.c_char_p * len(strings))(*[s.encode() for s in strings])
347-
348-
349-
def array_to_datetime(array: Sequence[Any] | np.ndarray) -> np.ndarray:
350-
"""
351-
Convert a 1-D datetime array from various types into numpy.datetime64.
352-
353-
If the input array is not in legal datetime formats, raise a ValueError exception.
354-
355-
Parameters
356-
----------
357-
array
358-
The input datetime array in various formats.
359-
360-
Supported types:
361-
362-
- str
363-
- numpy.datetime64
364-
- pandas.DateTimeIndex
365-
- datetime.datetime and datetime.date
366-
367-
Returns
368-
-------
369-
array
370-
1-D datetime array in numpy.datetime64.
371-
372-
Raises
373-
------
374-
ValueError
375-
If the datetime string is invalid.
376-
377-
Examples
378-
--------
379-
>>> import datetime
380-
>>> # numpy.datetime64 array
381-
>>> x = np.array(
382-
... ["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"],
383-
... dtype="datetime64[ns]",
384-
... )
385-
>>> array_to_datetime(x)
386-
array(['2010-06-01T00:00:00.000000000', '2011-06-01T12:00:00.000000000',
387-
'2012-01-01T12:34:56.000000000'], dtype='datetime64[ns]')
388-
389-
>>> # pandas.DateTimeIndex array
390-
>>> import pandas as pd
391-
>>> x = pd.date_range("2013", freq="YS", periods=3)
392-
>>> array_to_datetime(x)
393-
array(['2013-01-01T00:00:00.000000000', '2014-01-01T00:00:00.000000000',
394-
'2015-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
395-
396-
>>> # Python's built-in date and datetime
397-
>>> x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)]
398-
>>> array_to_datetime(x)
399-
array(['2018-01-01T00:00:00.000000', '2019-01-01T00:00:00.000000'],
400-
dtype='datetime64[us]')
401-
402-
>>> # Raw datetime strings in various format
403-
>>> x = [
404-
... "2018",
405-
... "2018-02",
406-
... "2018-03-01",
407-
... "2018-04-01T01:02:03",
408-
... ]
409-
>>> array_to_datetime(x)
410-
array(['2018-01-01T00:00:00', '2018-02-01T00:00:00',
411-
'2018-03-01T00:00:00', '2018-04-01T01:02:03'],
412-
dtype='datetime64[s]')
413-
414-
>>> # Mixed datetime types
415-
>>> x = [
416-
... "2018-01-01",
417-
... np.datetime64("2018-01-01"),
418-
... datetime.datetime(2018, 1, 1),
419-
... ]
420-
>>> array_to_datetime(x)
421-
array(['2018-01-01T00:00:00.000000', '2018-01-01T00:00:00.000000',
422-
'2018-01-01T00:00:00.000000'], dtype='datetime64[us]')
423-
"""
424-
return np.asarray(array, dtype=np.datetime64)

pygmt/clib/session.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import pandas as pd
1818
import xarray as xr
1919
from pygmt.clib.conversion import (
20-
array_to_datetime,
2120
dataarray_to_matrix,
2221
sequence_to_ctypes_array,
2322
strings_to_ctypes_array,
@@ -934,11 +933,6 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
934933
msg = f"Expected a numpy {ndim}-D array, got {array.ndim}-D."
935934
raise GMTInvalidInput(msg)
936935

937-
# For 1-D arrays, try to convert unknown object type to np.datetime64.
938-
if ndim == 1 and array.dtype.type is np.object_:
939-
with contextlib.suppress(ValueError):
940-
array = array_to_datetime(array)
941-
942936
# 1-D arrays can be numeric or text, 2-D arrays can only be numeric.
943937
valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC
944938
if (dtype := array.dtype.type) not in valid_dtypes:
@@ -993,7 +987,7 @@ def put_vector(
993987
gmt_type = self._check_dtype_and_dim(vector, ndim=1)
994988
if gmt_type in {self["GMT_TEXT"], self["GMT_DATETIME"]}:
995989
if gmt_type == self["GMT_DATETIME"]:
996-
vector = np.datetime_as_string(array_to_datetime(vector))
990+
vector = np.datetime_as_string(vector)
997991
vector_pointer = strings_to_ctypes_array(vector)
998992
else:
999993
vector_pointer = vector.ctypes.data_as(ctp.c_void_p)

0 commit comments

Comments
 (0)