Skip to content

Commit 6766b84

Browse files
seismanweiji14
andauthored
clib.conversion._to_numpy: Add tests for PyArrow's timestamp types (#3621)
Co-authored-by: Wei Ji <[email protected]>
1 parent f8e28ce commit 6766b84

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pygmt/tests/test_clib_to_numpy.py

+54
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818

1919
_HAS_PYARROW = True
2020
except ImportError:
21+
22+
class pa: # noqa: N801
23+
"""
24+
A dummy class to mimic pyarrow.
25+
"""
26+
27+
@staticmethod
28+
def timestamp(unit: str, tz: str | None = None):
29+
"""
30+
A dummy function to mimic pyarrow.timestamp.
31+
"""
32+
2133
_HAS_PYARROW = False
2234

2335

@@ -244,6 +256,7 @@ def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
244256
# - Date types:
245257
# - date32[day]
246258
# - date64[ms]
259+
# - Timestamp types: timestamp[unit], timestamp[unit, tz]
247260
#
248261
# In PyArrow, array types can be specified in two ways:
249262
#
@@ -366,3 +379,44 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype):
366379
result,
367380
np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
368381
)
382+
383+
384+
@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed")
385+
@pytest.mark.parametrize(
386+
("dtype", "expected_dtype"),
387+
[
388+
pytest.param(None, "datetime64[us]", id="None"),
389+
pytest.param("timestamp[s]", "datetime64[s]", id="timestamp[s]"),
390+
pytest.param("timestamp[ms]", "datetime64[ms]", id="timestamp[ms]"),
391+
pytest.param("timestamp[us]", "datetime64[us]", id="timestamp[us]"),
392+
pytest.param("timestamp[ns]", "datetime64[ns]", id="timestamp[ns]"),
393+
pytest.param(
394+
pa.timestamp("s", tz="UTC"), "datetime64[s]", id="timestamp[s, tz=UTC]"
395+
), # pa.timestamp with tz has no string alias.
396+
pytest.param(
397+
pa.timestamp("s", tz="America/New_York"),
398+
"datetime64[s]",
399+
id="timestamp[s, tz=America/New_York]",
400+
),
401+
pytest.param(
402+
pa.timestamp("s", tz="+07:30"),
403+
"datetime64[s]",
404+
id="timestamp[s, tz=+07:30]",
405+
),
406+
],
407+
)
408+
def test_to_numpy_pyarrow_timestamp(dtype, expected_dtype):
409+
"""
410+
Test the _to_numpy function with PyArrow arrays of PyArrow timestamp types.
411+
412+
pyarrow.timestamp(unit, tz=None) can accept units "s", "ms", "us", and "ns".
413+
414+
Reference: https://arrow.apache.org/docs/python/generated/pyarrow.timestamp.html
415+
"""
416+
data = [datetime(2024, 1, 2, 3, 4, 5), datetime(2024, 1, 2, 3, 4, 6)]
417+
array = pa.array(data, type=dtype)
418+
result = _to_numpy(array)
419+
_check_result(result, np.datetime64)
420+
assert result.dtype == expected_dtype
421+
assert result[0] == np.datetime64("2024-01-02T03:04:05")
422+
assert result[1] == np.datetime64("2024-01-02T03:04:06")

0 commit comments

Comments
 (0)