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._to_numpy: Add tests for Python sequence of datetime-like objects #3758

Merged
merged 13 commits into from
Jan 13, 2025
Merged
5 changes: 5 additions & 0 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@

array = np.ascontiguousarray(data, dtype=numpy_dtype)

# Check if a np.object_ or np.str_ array can be converted to np.datetime64.
if array.dtype.type in {np.object_, np.str_}:
with contextlib.suppress(TypeError, ValueError):

Check warning on line 200 in pygmt/clib/conversion.py

View check run for this annotation

Codecov / codecov/patch

pygmt/clib/conversion.py#L198-L200

Added lines #L198 - L200 were not covered by tests
return np.ascontiguousarray(array, dtype=np.datetime64)

# Check if a np.object_ array can be converted to np.str_.
if array.dtype == np.object_:
with contextlib.suppress(TypeError, ValueError):
Expand Down
24 changes: 24 additions & 0 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ def test_to_numpy_python_types(data, expected_dtype):
npt.assert_array_equal(result, data)


@pytest.mark.parametrize(
"data",
[
pytest.param(
["2018", "2018-02", "2018-03-01", "2018-04-01T01:02:03"], id="iso8601"
),
pytest.param(
[datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)],
id="datetime",
),
pytest.param(
["2018-01-01", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)],
id="mixed",
seisman marked this conversation as resolved.
Show resolved Hide resolved
),
seisman marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_to_numpy_python_datetime(data):
"""
Test the _to_numpy function with Python built-in datetime types.
"""
result = _to_numpy(data)
assert result.dtype.type == np.datetime64
seisman marked this conversation as resolved.
Show resolved Hide resolved


########################################################################################
# Test the _to_numpy function with NumPy arrays.
#
Expand Down
Loading