Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clib.conversion: Remove the unused array_to_datetime function #3507

Merged
merged 31 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3661e54
Refactor vectors_to_arrays and deprecate the array_to_datetime function
seisman Oct 11, 2024
20b9215
No need to use pd.api.types.is_string_dtype anymore
seisman Oct 13, 2024
83673cf
Add tests for vectors_to_arrays
seisman Oct 14, 2024
56a0841
Rename test_clib_conversion.py to test_clib_vectors_to_arrays.py
seisman Oct 14, 2024
6338cde
Add one line of code back
seisman Oct 14, 2024
1864556
Explicitly checking array.dtype.type
seisman Oct 14, 2024
54160bf
Correctly skip the tests if pyarrow is not installed
seisman Oct 14, 2024
f4e1a5f
Explicitly specify how to convert pandas/pyarrow string dtype to nump…
seisman Oct 14, 2024
ed3be20
Ensure the resulting numpy dtypes are supported by GMT
seisman Oct 14, 2024
8ab6c6c
Merge branch 'main' into remove/array-to-datetime
seisman Oct 17, 2024
e26afbf
Revert any changes that enhances the conversion process
seisman Oct 17, 2024
be3c93e
Rename array_to_datetime to _array_to_datetime
seisman Oct 17, 2024
3d40687
Merge branch 'main' into remove/array-to-datetime
seisman Oct 19, 2024
8f99a97
Some numpy dtypes like np.float16 can't be recognized by GMT
seisman Oct 19, 2024
5ecf445
Merge branch 'main' into remove/array-to-datetime
seisman Oct 22, 2024
cd75c5c
Merge branch 'main' into remove/array-to-datetime
seisman Oct 23, 2024
91e10a7
Merge branch 'main' into remove/array-to-datetime
seisman Oct 24, 2024
47214e5
Merge branch 'main' into remove/array-to-datetime
seisman Oct 24, 2024
d8777e5
Check three variants for string dtypes
seisman Oct 24, 2024
3286f08
Merge branch 'main' into remove/array-to-datetime
seisman Oct 28, 2024
38b839a
Also check TypeError
seisman Oct 28, 2024
76cb837
Merge branch 'main' into remove/array-to-datetime
seisman Oct 31, 2024
9573fb6
Merge branch 'main' into remove/array-to-datetime
seisman Nov 15, 2024
bccae94
Remove the tests for pandas string dtypes
seisman Nov 15, 2024
d64c795
Merge branch 'main' into remove/array-to-datetime
seisman Nov 28, 2024
a410357
Remove the array_to_datetime function
seisman Nov 28, 2024
53288dc
Merge branch 'main' into remove/array-to-datetime
seisman Jan 9, 2025
765d6c3
Merge branch 'main' into remove/array-to-datetime
seisman Jan 13, 2025
d847eee
Merge branch 'main' into remove/array-to-datetime
seisman Feb 1, 2025
b0d4029
Merge branch 'main' into remove/array-to-datetime
seisman Feb 3, 2025
9743bbf
Merge branch 'main' into remove/array-to-datetime
seisman Feb 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 0 additions & 78 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These doctests are already covered by the tests in test_clib_to_numpy.py

>>> 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)
8 changes: 1 addition & 7 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Comment on lines -937 to -940
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the _to_numpy function in PR #3760.


# 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:
Expand Down Expand Up @@ -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)
Expand Down
Loading