Skip to content

Commit

Permalink
clib.conversion._to_numpy: Add tests for PyArrow's timestamp type
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Nov 15, 2024
1 parent 09802b0 commit ce2b830
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import pyarrow as pa

_HAS_PYARROW = True
pa_timestamp = pa.timestamp
except ImportError:
_HAS_PYARROW = False
pa_timestamp = None


def _check_result(result, expected_dtype):
Expand Down Expand Up @@ -235,6 +237,7 @@ def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
# - Date types:
# - date32[day]
# - date64[ms]
# - Datetime types: timestamp
#
# In PyArrow, array types can be specified in two ways:
#
Expand Down Expand Up @@ -357,3 +360,43 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype):
result,
np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
)


@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed")
@pytest.mark.parametrize(
("dtype", "expected_dtype"),
[
pytest.param(None, "datetime64[us]", id="None"),
pytest.param("timestamp[s]", "datetime64[s]", id="timestamp[s]"),
pytest.param("timestamp[ms]", "datetime64[ms]", id="timestamp[ms]"),
pytest.param("timestamp[us]", "datetime64[us]", id="timestamp[us]"),
pytest.param("timestamp[ns]", "datetime64[ns]", id="timestamp[ns]"),
pytest.param(
pa_timestamp("s", tz="UTC"), "datetime64[s]", id="timestamp[s, tz=UTC]"
), # pa.timestamp with tz has no string alias.
pytest.param(
pa_timestamp("s", tz="America/New_York"),
"datetime64[s]",
id="timestamp[s, tz=America/New_York]",
),
pytest.param(
pa_timestamp("s", tz="+07:30"),
"datetime64[s]",
id="timestamp[s, tz=+07:30]",
),
],
)
def test_to_numpy_pyarrow_timestamp(dtype, expected_dtype):
"""
Test the _to_numpy function with PyArrow arrays of PyArrow datetime types.
pyarrow.timestamp(unit, tz=None) can accept units "s", "ms", "us", and "ns".
Reference: https://arrow.apache.org/docs/python/generated/pyarrow.timestamp.html
"""
data = [datetime(2024, 1, 2, 3, 4, 5), datetime(2024, 1, 2, 3, 4, 6)]
array = pa.array(data, type=dtype)
result = _to_numpy(array)
_check_result(result, np.datetime64)
assert result.dtype == expected_dtype
npt.assert_array_equal(result, array)

0 comments on commit ce2b830

Please sign in to comment.