|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import enum |
| 4 | +from contextlib import AbstractContextManager, nullcontext as does_not_warn |
4 | 5 | from datetime import datetime, timedelta, timezone |
5 | 6 | from typing import TYPE_CHECKING, Any, Literal |
6 | 7 |
|
|
17 | 18 | from collections.abc import Iterable |
18 | 19 |
|
19 | 20 | from narwhals.typing import IntoSeries, NonNestedDType |
20 | | - from tests.utils import Constructor |
| 21 | + from tests.utils import Constructor, ConstructorPandasLike |
21 | 22 |
|
22 | 23 |
|
23 | 24 | @pytest.mark.parametrize("time_unit", ["us", "ns", "ms"]) |
@@ -558,3 +559,26 @@ def test_dtype_base_type_nested() -> None: |
558 | 559 | assert nw.Array.base_type() is nw.Array(nw.String, 2).base_type() |
559 | 560 | assert nw.Struct.base_type() is nw.Struct({"a": nw.Boolean}).base_type() |
560 | 561 | assert nw.Enum.base_type() is nw.Enum(["beluga", "narwhal"]).base_type() |
| 562 | + |
| 563 | + |
| 564 | +@pytest.mark.parametrize( |
| 565 | + ("dtype", "context"), |
| 566 | + [ |
| 567 | + (nw.Datetime("ns"), does_not_warn()), |
| 568 | + (nw.Datetime, does_not_warn()), |
| 569 | + (nw.Datetime(), pytest.warns(UserWarning, match="time_unit")), |
| 570 | + (nw.Datetime("us"), pytest.warns(UserWarning, match="time_unit='us'")), |
| 571 | + (nw.Datetime("s"), pytest.warns(UserWarning, match="time_unit='s'")), |
| 572 | + ], |
| 573 | +) |
| 574 | +def test_pandas_datetime_ignored_time_unit_warns( |
| 575 | + constructor_pandas_like: ConstructorPandasLike, |
| 576 | + dtype: nw.Datetime | type[nw.Datetime], |
| 577 | + context: AbstractContextManager[Any], |
| 578 | +) -> None: |
| 579 | + data = {"a": [datetime(2001, 1, 1), None, datetime(2001, 1, 3)]} |
| 580 | + expr = nw.col("a").cast(dtype) |
| 581 | + df = nw.from_native(constructor_pandas_like(data)) |
| 582 | + ctx = does_not_warn() if PANDAS_VERSION >= (2,) else context |
| 583 | + with ctx: |
| 584 | + df.select(expr) |
0 commit comments