|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import sys
|
| 6 | +from datetime import date, datetime |
6 | 7 |
|
7 | 8 | import numpy as np
|
8 | 9 | import numpy.testing as npt
|
@@ -204,6 +205,9 @@ def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
|
204 | 205 | # - int8, int16, int32, int64
|
205 | 206 | # - uint8, uint16, uint32, uint64
|
206 | 207 | # - float16, float32, float64
|
| 208 | +# - Date types: |
| 209 | +# - date32[day] |
| 210 | +# - date64[ms] |
207 | 211 | #
|
208 | 212 | # In PyArrow, array types can be specified in two ways:
|
209 | 213 | #
|
@@ -272,3 +276,35 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_numeric_with_na(dtype, expected_d
|
272 | 276 | result = _to_numpy(array)
|
273 | 277 | _check_result(result, expected_dtype)
|
274 | 278 | npt.assert_array_equal(result, array)
|
| 279 | + |
| 280 | + |
| 281 | +@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed") |
| 282 | +@pytest.mark.parametrize( |
| 283 | + ("dtype", "expected_dtype"), |
| 284 | + [ |
| 285 | + pytest.param("date32[day]", "datetime64[D]", id="date32[day]"), |
| 286 | + pytest.param("date64[ms]", "datetime64[ms]", id="date64[ms]"), |
| 287 | + ], |
| 288 | +) |
| 289 | +def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype): |
| 290 | + """ |
| 291 | + Test the _to_numpy function with PyArrow arrays of PyArrow date types. |
| 292 | +
|
| 293 | + date32[day] and date64[ms] are stored as 32-bit and 64-bit integers, respectively, |
| 294 | + representing the number of days and milliseconds since the UNIX epoch (1970-01-01). |
| 295 | +
|
| 296 | + Here we explicitly check the dtype and date unit of the result. |
| 297 | + """ |
| 298 | + data = [ |
| 299 | + date(2024, 1, 1), |
| 300 | + datetime(2024, 1, 2), |
| 301 | + datetime(2024, 1, 3), |
| 302 | + ] |
| 303 | + array = pa.array(data, type=dtype) |
| 304 | + result = _to_numpy(array) |
| 305 | + _check_result(result, np.datetime64) |
| 306 | + assert result.dtype == expected_dtype # Explicitly check the date unit. |
| 307 | + npt.assert_array_equal( |
| 308 | + result, |
| 309 | + np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype), |
| 310 | + ) |
0 commit comments