Skip to content

Commit

Permalink
Add more tests by Wei Ji
Browse files Browse the repository at this point in the history
Co-authored-by: Wei Ji <[email protected]>
  • Loading branch information
seisman and weiji14 committed Jan 9, 2025
1 parent ada31d8 commit 0eba5d2
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Tests for the _to_numpy function in the clib.conversion module.
"""

import datetime
import sys
from datetime import date, datetime

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -87,11 +87,30 @@ def test_to_numpy_python_types(data, expected_dtype):
["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)],
[
datetime.date(2018, 1, 1),
datetime.datetime(2018, 2, 1),
datetime.date(2018, 3, 1),
datetime.datetime(2018, 4, 1, 1, 2, 3),
],
id="datetime",
),
pytest.param(
["2018-01-01", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)],
[
pd.Timestamp("2018-01-01"),
pd.Timestamp("2018-02-01"),
pd.Timestamp("2018-03-01"),
pd.Timestamp("2018-04-01T01:02:03"),
],
id="pd_timestamp",
),
pytest.param(
[
"2018-01-01",
np.datetime64("2018-02-01"),
datetime.datetime(2018, 3, 1),
pd.Timestamp("2018-04-01T01:02:03"),
],
id="mixed",
),
],
Expand All @@ -102,6 +121,18 @@ def test_to_numpy_python_datetime(data):
"""
result = _to_numpy(data)
assert result.dtype.type == np.datetime64
npt.assert_array_equal(
result,
np.array(
[
"2018-01-01T00:00:00",
"2018-02-01T00:00:00",
"2018-03-01T00:00:00",
"2018-04-01T01:02:03",
],
dtype="datetime64[s]",
),
)


########################################################################################
Expand Down Expand Up @@ -520,9 +551,9 @@ def test_to_numpy_pyarrow_date(dtype, expected_dtype):
Here we explicitly check the dtype and date unit of the result.
"""
data = [
date(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
datetime.date(2024, 1, 1),
datetime.datetime(2024, 1, 2),
datetime.datetime(2024, 1, 3),
]
array = pa.array(data, type=dtype)
result = _to_numpy(array)
Expand Down Expand Up @@ -566,7 +597,10 @@ def test_to_numpy_pyarrow_timestamp(dtype, expected_dtype):
Reference: https://arrow.apache.org/docs/python/generated/pyarrow.timestamp.html
"""
data = [datetime(2024, 1, 2, 3, 4, 5), datetime(2024, 1, 2, 3, 4, 6)]
data = [
datetime.datetime(2024, 1, 2, 3, 4, 5),
datetime.datetime(2024, 1, 2, 3, 4, 6),
]
array = pa.array(data, type=dtype)
result = _to_numpy(array)
_check_result(result, np.datetime64)
Expand Down

0 comments on commit 0eba5d2

Please sign in to comment.