From 5945581343f8619aef14fd0d0cc04b2c48e4ce08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Tue, 2 Dec 2025 18:58:07 +0100 Subject: [PATCH 1/2] enh: Add support for `slice(None)` in series getitem --- narwhals/_utils.py | 9 ++++++++- tests/series_only/getitem_test.py | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/narwhals/_utils.py b/narwhals/_utils.py index 8edffcc5cb..6ba4267085 100644 --- a/narwhals/_utils.py +++ b/narwhals/_utils.py @@ -1318,7 +1318,11 @@ def is_slice_index(obj: Any) -> TypeIs[_SliceIndex]: return isinstance(obj, slice) and ( isinstance(obj.start, int) or isinstance(obj.stop, int) - or (isinstance(obj.step, int) and obj.start is None and obj.stop is None) + or ( + isinstance(obj.step, (int, NoneType)) + and obj.start is None + and obj.stop is None + ) ) @@ -2145,3 +2149,6 @@ def __repr__(self) -> str: # pragma: no cover # the "no_default" sentinel should typically be used when one of the valid parameter # values is None, as otherwise we cannot determine if the caller has set that value. no_default = _NoDefault.no_default + +# Can be imported from types in Python 3.10 +NoneType = type(None) diff --git a/tests/series_only/getitem_test.py b/tests/series_only/getitem_test.py index cecb8099ee..eebeaff6b5 100644 --- a/tests/series_only/getitem_test.py +++ b/tests/series_only/getitem_test.py @@ -5,7 +5,7 @@ import pytest import narwhals as nw -from tests.utils import assert_equal_data +from tests.utils import assert_equal_data, assert_equal_series if TYPE_CHECKING: from tests.utils import ConstructorEager @@ -38,6 +38,9 @@ def test_by_slice(constructor_eager: ConstructorEager) -> None: result = {"b": df[[], 1]} expected = {"b": []} assert_equal_data(result, expected) + result = df[:, 0][slice(None)] + expected = [1, 2, 3] + assert_equal_series(result, expected, name="a") def test_getitem_arrow_scalar() -> None: From fff0462adee66ff81ce46e94d621b580171ad9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Tue, 2 Dec 2025 19:11:22 +0100 Subject: [PATCH 2/2] fix typing --- tests/series_only/getitem_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/series_only/getitem_test.py b/tests/series_only/getitem_test.py index eebeaff6b5..aa63ac8414 100644 --- a/tests/series_only/getitem_test.py +++ b/tests/series_only/getitem_test.py @@ -38,9 +38,9 @@ def test_by_slice(constructor_eager: ConstructorEager) -> None: result = {"b": df[[], 1]} expected = {"b": []} assert_equal_data(result, expected) - result = df[:, 0][slice(None)] - expected = [1, 2, 3] - assert_equal_series(result, expected, name="a") + result_ser = df[:, 0][slice(None)] + expected_ser = [1, 2, 3] + assert_equal_series(result_ser, expected_ser, name="a") def test_getitem_arrow_scalar() -> None: