Skip to content

Commit a64e9e3

Browse files
committed
Add tests for pandas datetime dtypes
1 parent bd7fc96 commit a64e9e3

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

pygmt/tests/test_clib_to_numpy.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,68 @@ def test_to_numpy_pandas_date(dtype, expected_dtype):
331331
)
332332

333333

334+
@pytest.mark.parametrize(
335+
("dtype", "expected_dtype"),
336+
[
337+
# NumPy datetime64 types. Only unit 's'/'ms'/'us'/'ns' are supported.
338+
pytest.param("datetime64[s]", "datetime64[s]", id="datetime64[s]"),
339+
pytest.param("datetime64[ms]", "datetime64[ms]", id="datetime64[ms]"),
340+
pytest.param("datetime64[us]", "datetime64[us]", id="datetime64[us]"),
341+
pytest.param("datetime64[ns]", "datetime64[ns]", id="datetime64[ns]"),
342+
# pandas.DatetimeTZDtype can be given in two ways:
343+
# 1. pandas.DatetimeTZDtype(unit, tz)
344+
# 2. String aliases: "datetime64[unit, tz]"
345+
pytest.param("datetime64[s, UTC]", "datetime64[s]", id="datetime64[s, tz=UTC]"),
346+
pytest.param(
347+
"datetime64[s, America/New_York]",
348+
"datetime64[s]",
349+
id="datetime64[s, tz=America/New_York]",
350+
),
351+
pytest.param(
352+
"datetime64[s, +07:30]", "datetime64[s]", id="datetime64[s, +07:30]"
353+
),
354+
# PyArrow timestamp types can be given in two ways:
355+
# 1. pd.ArrowDtype(pyarrow.Timestamp(unit, tz=tz))
356+
# 2. String aliases: "timestamp[unit, tz][pyarrow]"
357+
pytest.param(
358+
"timestamp[s, UTC][pyarrow]",
359+
"datetime64[s]",
360+
id="timestamp[s, UTC][pyarrow]",
361+
marks=skip_if_no(package="pyarrow"),
362+
),
363+
pytest.param(
364+
"timestamp[s, America/New_York][pyarrow]",
365+
"datetime64[s]",
366+
id="timestamp[s, America/New_York][pyarrow]",
367+
marks=skip_if_no(package="pyarrow"),
368+
),
369+
pytest.param(
370+
"timestamp[s, +08:00][pyarrow]",
371+
"datetime64[s]",
372+
id="timestamp[s, +08:00][pyarrow]",
373+
marks=skip_if_no(package="pyarrow"),
374+
),
375+
],
376+
)
377+
def test_to_numpy_pandas_datetime(dtype, expected_dtype):
378+
"""
379+
Test the _to_numpy function with pandas.Series of datetime types.
380+
"""
381+
series = pd.Series(
382+
[pd.Timestamp("2024-01-02T03:04:05"), pd.Timestamp("2024-01-02T03:04:06")],
383+
dtype=dtype,
384+
)
385+
result = _to_numpy(series)
386+
_check_result(result, np.datetime64)
387+
assert result.dtype == expected_dtype
388+
389+
if "," in str(dtype): # A hacky solution to decide if the dtype is timezone-aware.
390+
series = series.dt.tz_convert("UTC") # Convert to UTC if timezone-aware.
391+
expected_series = series.dt.strftime("%Y-%m-%dT%H:%M:%S").to_list()
392+
393+
npt.assert_array_equal(result, np.array(expected_series, dtype=expected_dtype))
394+
395+
334396
########################################################################################
335397
# Test the _to_numpy function with PyArrow arrays.
336398
#

0 commit comments

Comments
 (0)