From 65baa30933d6be60f8cc0fb78604e98f4e6a8d43 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 10 Nov 2024 21:54:20 +0800 Subject: [PATCH 1/2] clib.conversion._to_numpy: Add tests for panda.Series with pyarrow date32/date64 types --- pygmt/tests/test_clib_to_numpy.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pygmt/tests/test_clib_to_numpy.py b/pygmt/tests/test_clib_to_numpy.py index 3624ed2be8d..cbca87a8856 100644 --- a/pygmt/tests/test_clib_to_numpy.py +++ b/pygmt/tests/test_clib_to_numpy.py @@ -161,6 +161,28 @@ def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, expected_dtype): npt.assert_array_equal(result, series) +@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed") +@pytest.mark.parametrize( + ("dtype", "expected_dtype"), + [ + pytest.param("date32[day][pyarrow]", "datetime64[D]", id="date32[day]"), + pytest.param("date64[ms][pyarrow]", "datetime64[ms]", id="date64[ms]"), + ], +) +def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype): + """ + Test the _to_numpy function with pandas.Series of PyArrow date32/date64 types. + """ + series = pd.Series(pd.date_range(start="2024-01-01", periods=3), dtype=dtype) + result = _to_numpy(series) + _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), + ) + + ######################################################################################## # Test the _to_numpy function with PyArrow arrays. # From af303863cfdb817e6f92555f96dca94fb4e96478 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 10 Nov 2024 21:55:37 +0800 Subject: [PATCH 2/2] Convert 'date32[day][pyarrow]' to 'datetime64[D]' and 'date64[ms][pyarrow]' to 'datetime64[ms]' --- pygmt/clib/conversion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index af8eb3458d4..59fa0d584cd 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -157,9 +157,9 @@ def _to_numpy(data: Any) -> np.ndarray: The C contiguous NumPy array. """ # Mapping of unsupported dtypes to the expected NumPy dtype. - dtypes: dict[str, type] = { - "date32[day][pyarrow]": np.datetime64, - "date64[ms][pyarrow]": np.datetime64, + dtypes: dict[str, str | type] = { + "date32[day][pyarrow]": "datetime64[D]", + "date64[ms][pyarrow]": "datetime64[ms]", } if (