From f4df861e43aa67b4a924cb27f3ebb03ab9db64f4 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 5 Jan 2025 17:16:58 +0000 Subject: [PATCH] fix: nw.lit(date, dtype=nw.Date) --- narwhals/_pandas_like/utils.py | 5 +---- narwhals/expr.py | 2 +- narwhals/stable/v1/__init__.py | 2 +- tests/{frame => expr_and_series}/lit_test.py | 7 +++++++ tests/series_only/cast_test.py | 12 ------------ 5 files changed, 10 insertions(+), 18 deletions(-) rename tests/{frame => expr_and_series}/lit_test.py (91%) diff --git a/narwhals/_pandas_like/utils.py b/narwhals/_pandas_like/utils.py index 655e60773..24760d783 100644 --- a/narwhals/_pandas_like/utils.py +++ b/narwhals/_pandas_like/utils.py @@ -637,10 +637,7 @@ def narwhals_to_native_dtype( # noqa: PLR0915 else f"timedelta64[{du_time_unit}]" ) if isinstance_or_issubclass(dtype, dtypes.Date): - if dtype_backend == "pyarrow-nullable": - return "date32[pyarrow]" - msg = "Date dtype only supported for pyarrow-backed data types in pandas" - raise NotImplementedError(msg) + return "date32[pyarrow]" if isinstance_or_issubclass(dtype, dtypes.Enum): msg = "Converting to Enum is not (yet) supported" raise NotImplementedError(msg) diff --git a/narwhals/expr.py b/narwhals/expr.py index 0ab7ba20e..aa934a01f 100644 --- a/narwhals/expr.py +++ b/narwhals/expr.py @@ -7023,7 +7023,7 @@ def all_horizontal(*exprs: IntoExpr | Iterable[IntoExpr]) -> Expr: ) -def lit(value: Any, dtype: DType | None = None) -> Expr: +def lit(value: Any, dtype: DType | type[DType] | None = None) -> Expr: """Return an expression representing a literal value. Arguments: diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py index ba5117425..5ffc475e5 100644 --- a/narwhals/stable/v1/__init__.py +++ b/narwhals/stable/v1/__init__.py @@ -2542,7 +2542,7 @@ def len() -> Expr: return _stableify(nw.len()) -def lit(value: Any, dtype: DType | None = None) -> Expr: +def lit(value: Any, dtype: DType | type[DType] | None = None) -> Expr: """Return an expression representing a literal value. Arguments: diff --git a/tests/frame/lit_test.py b/tests/expr_and_series/lit_test.py similarity index 91% rename from tests/frame/lit_test.py rename to tests/expr_and_series/lit_test.py index 8b3bcd8e2..346a6c727 100644 --- a/tests/frame/lit_test.py +++ b/tests/expr_and_series/lit_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +from datetime import date from typing import TYPE_CHECKING from typing import Any @@ -89,3 +90,9 @@ def test_lit_operation( result = df.select(expr.alias(col_name)) expected = {col_name: expected_result} assert_equal_data(result, expected) + + +def test_date_lit(constructor: Constructor) -> None: + df = nw.from_native(constructor({"a": [1]})) + result = df.with_columns(nw.lit(date(2020, 1, 1), dtype=nw.Date)).collect_schema() + assert result == {"a": nw.Int64, "literal": nw.Date} diff --git a/tests/series_only/cast_test.py b/tests/series_only/cast_test.py index 10587a084..b4051e503 100644 --- a/tests/series_only/cast_test.py +++ b/tests/series_only/cast_test.py @@ -98,18 +98,6 @@ def test_cast_date_datetime_pandas() -> None: assert df.schema == {"a": nw.Date} -@pytest.mark.skipif( - PANDAS_VERSION < (2, 0, 0), - reason="pyarrow dtype not available", -) -def test_cast_date_datetime_invalid() -> None: - # pandas: pyarrow datetime to date - dfpd = pd.DataFrame({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}) - df = nw.from_native(dfpd) - with pytest.raises(NotImplementedError, match="pyarrow"): - df.select(nw.col("a").cast(nw.Date)) - - @pytest.mark.filterwarnings("ignore: casting period") def test_unknown_to_int() -> None: df = pd.DataFrame({"a": pd.period_range("2000", periods=3, freq="min")})