Skip to content

Commit

Permalink
add support for PyArrow: multi-element __getitem__ (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
raisadz authored Sep 11, 2024
1 parent ba8238a commit d6f3cd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def __getitem__(self, idx: slice | Sequence[int]) -> Self: ...
def __getitem__(self, idx: int | slice | Sequence[int]) -> Any | Self:
if isinstance(idx, int):
return self._native_series[idx]
if isinstance(idx, Sequence):
return self._from_native_series(self._native_series.take(idx))
return self._from_native_series(self._native_series[idx])

def scatter(self, indices: int | Sequence[int], values: Any) -> Self:
Expand Down
15 changes: 15 additions & 0 deletions tests/series_only/slice_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Any

import narwhals.stable.v1 as nw
from tests.utils import compare_dicts


def test_slice(constructor_eager: Any) -> None:
data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [1, 4, 2]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = {"a": df["a"][[0, 1]]}
expected = {"a": [1, 2]}
compare_dicts(result, expected)
result = {"a": df["a"][1:]}
expected = {"a": [2, 3]}
compare_dicts(result, expected)

0 comments on commit d6f3cd7

Please sign in to comment.