Skip to content

Commit 66b22dc

Browse files
authored
clib.conversion._to_numpy: Add tests for pyarrow.array with date32/date64 dtype (#3609)
1 parent 1475837 commit 66b22dc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pygmt/tests/test_clib_to_numpy.py

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import sys
6+
from datetime import date, datetime
67

78
import numpy as np
89
import numpy.testing as npt
@@ -204,6 +205,9 @@ def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
204205
# - int8, int16, int32, int64
205206
# - uint8, uint16, uint32, uint64
206207
# - float16, float32, float64
208+
# - Date types:
209+
# - date32[day]
210+
# - date64[ms]
207211
#
208212
# In PyArrow, array types can be specified in two ways:
209213
#
@@ -272,3 +276,35 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_numeric_with_na(dtype, expected_d
272276
result = _to_numpy(array)
273277
_check_result(result, expected_dtype)
274278
npt.assert_array_equal(result, array)
279+
280+
281+
@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed")
282+
@pytest.mark.parametrize(
283+
("dtype", "expected_dtype"),
284+
[
285+
pytest.param("date32[day]", "datetime64[D]", id="date32[day]"),
286+
pytest.param("date64[ms]", "datetime64[ms]", id="date64[ms]"),
287+
],
288+
)
289+
def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype):
290+
"""
291+
Test the _to_numpy function with PyArrow arrays of PyArrow date types.
292+
293+
date32[day] and date64[ms] are stored as 32-bit and 64-bit integers, respectively,
294+
representing the number of days and milliseconds since the UNIX epoch (1970-01-01).
295+
296+
Here we explicitly check the dtype and date unit of the result.
297+
"""
298+
data = [
299+
date(2024, 1, 1),
300+
datetime(2024, 1, 2),
301+
datetime(2024, 1, 3),
302+
]
303+
array = pa.array(data, type=dtype)
304+
result = _to_numpy(array)
305+
_check_result(result, np.datetime64)
306+
assert result.dtype == expected_dtype # Explicitly check the date unit.
307+
npt.assert_array_equal(
308+
result,
309+
np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
310+
)

0 commit comments

Comments
 (0)