From 795796dff248b9c11da2685ed61e64addba24d2b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 10 Nov 2024 17:39:27 +0800 Subject: [PATCH] clib.conversion._to_numpy: Add tests for pyarrow.array with date32/date64 dtype --- pygmt/tests/test_clib_to_numpy.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pygmt/tests/test_clib_to_numpy.py b/pygmt/tests/test_clib_to_numpy.py index 3624ed2be8d..371e1749ea0 100644 --- a/pygmt/tests/test_clib_to_numpy.py +++ b/pygmt/tests/test_clib_to_numpy.py @@ -3,6 +3,7 @@ """ import sys +from datetime import date, datetime import numpy as np import numpy.testing as npt @@ -170,6 +171,9 @@ def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, expected_dtype): # - int8, int16, int32, int64 # - uint8, uint16, uint32, uint64 # - float16, float32, float64 +# - Date types: +# - date32[day] +# - date64[ms] # # In PyArrow, array types can be specified in two ways: # @@ -238,3 +242,35 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_numeric_with_na(dtype, expected_d result = _to_numpy(array) _check_result(result, expected_dtype) npt.assert_array_equal(result, array) + + +@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed") +@pytest.mark.parametrize( + ("dtype", "expected_dtype"), + [ + pytest.param("date32[day]", "datetime64[D]", id="date32[day]"), + pytest.param("date64[ms]", "datetime64[ms]", id="date64[ms]"), + ], +) +def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype): + """ + Test the _to_numpy function with PyArrow arrays of PyArrow date types. + + date32[day] and date64[ms] are stored as 32-bit and 64-bit integers, respectively, + representing the number of days and milliseconds since the UNIX epoch (1970-01-01). + + 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), + ] + array = pa.array(data, type=dtype) + result = _to_numpy(array) + _check_result(result, np.datetime64) + assert result.dtype == expected_dtype # Explicitly check the date unit. + npt.assert_array_equal( + result, + np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype), + )