|
18 | 18 |
|
19 | 19 | _HAS_PYARROW = True
|
20 | 20 | 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 | + |
21 | 33 | _HAS_PYARROW = False
|
22 | 34 |
|
23 | 35 |
|
@@ -244,6 +256,7 @@ def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
|
244 | 256 | # - Date types:
|
245 | 257 | # - date32[day]
|
246 | 258 | # - date64[ms]
|
| 259 | +# - Timestamp types: timestamp[unit], timestamp[unit, tz] |
247 | 260 | #
|
248 | 261 | # In PyArrow, array types can be specified in two ways:
|
249 | 262 | #
|
@@ -366,3 +379,44 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype):
|
366 | 379 | result,
|
367 | 380 | np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
|
368 | 381 | )
|
| 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