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 pandas.Series with pyarrow date32/date64 types #3610

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
22 changes: 22 additions & 0 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,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.
#
Expand Down
Loading