Skip to content

Commit 38266bf

Browse files
authored
test: cudf fixup, address pandas deprecationwarning (#2496)
1 parent 48eada7 commit 38266bf

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

narwhals/_dask/expr_dt.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import TYPE_CHECKING
44

55
from narwhals._duration import parse_interval_string
6+
from narwhals._pandas_like.utils import UNIT_DICT
67
from narwhals._pandas_like.utils import calculate_timestamp_date
78
from narwhals._pandas_like.utils import calculate_timestamp_datetime
89
from narwhals._pandas_like.utils import native_to_narwhals_dtype
@@ -160,13 +161,11 @@ def total_nanoseconds(self) -> DaskExpr:
160161
)
161162

162163
def truncate(self, every: str) -> DaskExpr:
163-
_, unit = parse_interval_string(every)
164+
multiple, unit = parse_interval_string(every)
164165
if unit in {"mo", "q", "y"}:
165166
msg = f"Truncating to {unit} is not supported yet for dask."
166167
raise NotImplementedError(msg)
167-
if unit == "m":
168-
every = every.replace("m", "min", 1)
168+
freq = f"{multiple}{UNIT_DICT.get(unit, unit)}"
169169
return self._compliant_expr._with_callable(
170-
lambda _input: _input.dt.floor(every),
171-
"truncate",
170+
lambda _input: _input.dt.floor(freq), "truncate"
172171
)

narwhals/_pandas_like/series_dt.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from narwhals._compliant.any_namespace import DateTimeNamespace
77
from narwhals._duration import parse_interval_string
8+
from narwhals._pandas_like.utils import UNIT_DICT
89
from narwhals._pandas_like.utils import PandasLikeSeriesNamespace
910
from narwhals._pandas_like.utils import calculate_timestamp_date
1011
from narwhals._pandas_like.utils import calculate_timestamp_datetime
@@ -196,6 +197,11 @@ def timestamp(self, time_unit: TimeUnit) -> PandasLikeSeries:
196197
def truncate(self, every: str) -> PandasLikeSeries:
197198
multiple, unit = parse_interval_string(every)
198199
native = self.native
200+
if self.implementation.is_cudf():
201+
if multiple != 1:
202+
msg = f"Only multiple `1` is supported for cuDF, got: {multiple}."
203+
raise NotImplementedError(msg)
204+
return self.with_native(self.native.dt.floor(UNIT_DICT.get(unit, unit)))
199205
dtype_backend = get_dtype_backend(native.dtype, self.compliant._implementation)
200206
if unit in {"mo", "q", "y"}:
201207
if self.implementation.is_cudf():
@@ -225,6 +231,6 @@ def truncate(self, every: str) -> PandasLikeSeries:
225231
result_arr, dtype=native.dtype, index=native.index, name=native.name
226232
)
227233
return self.with_native(result_native)
228-
if unit == "m":
229-
every = every.replace("m", "min", 1)
230-
return self.with_native(self.native.dt.floor(every))
234+
return self.with_native(
235+
self.native.dt.floor(f"{multiple}{UNIT_DICT.get(unit, unit)}")
236+
)

narwhals/_pandas_like/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
$"""
9191
PATTERN_PA_DURATION = re.compile(PA_DURATION_RGX, re.VERBOSE)
9292

93+
UNIT_DICT = {"d": "D", "m": "min"}
94+
9395

9496
def align_and_extract_native(
9597
lhs: PandasLikeSeries, rhs: PandasLikeSeries | object

tests/expr_and_series/dt/truncate_test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ def test_truncate_custom(
195195
)
196196
if every.endswith("ns") and any(x in str(constructor) for x in ("polars", "duckdb")):
197197
request.applymarker(pytest.mark.xfail())
198+
if "cudf" in str(constructor):
199+
# https://github.com/rapidsai/cudf/issues/18654
200+
request.applymarker(pytest.mark.xfail(reason="Not implemented"))
198201
if any(every.endswith(x) for x in ("mo", "q", "y")) and any(
199-
x in str(constructor) for x in ("dask", "cudf")
202+
x in str(constructor) for x in ("dask",)
200203
):
201204
request.applymarker(pytest.mark.xfail(reason="Not implemented"))
202205
df = nw.from_native(constructor(data))

tests/expr_and_series/str/to_uppercase_to_lowercase_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_str_to_uppercase(
3535
if (
3636
any(
3737
x in str(constructor)
38-
for x in ("pandas_constructor", "pandas_nullable", "polars")
38+
for x in ("pandas_constructor", "pandas_nullable", "polars", "cudf")
3939
)
4040
and "ẞ" in expected["a"][0]
4141
):
@@ -72,7 +72,7 @@ def test_str_to_uppercase_series(
7272
if (
7373
any(
7474
x in str(constructor_eager)
75-
for x in ("pandas_constructor", "pandas_nullable", "polars")
75+
for x in ("pandas_constructor", "pandas_nullable", "polars", "cudf")
7676
)
7777
and "ẞ" in expected["a"][0]
7878
):

0 commit comments

Comments
 (0)