Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing Series to Series.__getitem__ #1533

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions narwhals/_polars/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from narwhals._polars.dataframe import PolarsDataFrame
from narwhals.dtypes import DType
from narwhals.series import Series as NWSeries
from narwhals.utils import Version

T = TypeVar("T")
Expand Down Expand Up @@ -120,10 +121,14 @@ def dtype(self: Self) -> DType:
def __getitem__(self: Self, item: int) -> Any: ...

@overload
def __getitem__(self: Self, item: slice | Sequence[int]) -> Self: ...

def __getitem__(self: Self, item: int | slice | Sequence[int]) -> Any | Self:
return self._from_native_object(self._native_series.__getitem__(item))
def __getitem__(self: Self, item: slice | Sequence[int] | NWSeries[Any]) -> Self: ...

def __getitem__(
self: Self, item: int | slice | Sequence[int] | NWSeries[Any]
) -> Any | Self:
if isinstance(item, (int, slice, Sequence)):
return self._from_native_object(self._native_series.__getitem__(item))
return self._from_native_object(self._native_series.__getitem__(item.to_numpy()))
Copy link
Member

@MarcoGorelli MarcoGorelli Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ€” not sure we should be converting to numpy here

I'm not totally sure what we should be doing instead though, sorry, will need to investigate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I had doubts about this too and picked a solution that seemed to work without actually understanding the consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems using item.to_native() instead works, and also solves an issue with missing values becoming NaN with to_numpy(). Do you think it's acceptable? Any other corner cases to cover with tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - i'll check, to be careful we get the details right here


def cast(self: Self, dtype: DType) -> Self:
ser = self._native_series
Expand Down
6 changes: 4 additions & 2 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ def __array__(self: Self, dtype: Any = None, copy: bool | None = None) -> np.nda
def __getitem__(self: Self, idx: int) -> Any: ...

@overload
def __getitem__(self: Self, idx: slice | Sequence[int]) -> Self: ...
def __getitem__(self: Self, idx: slice | Sequence[int] | Series[Any]) -> Self: ...

def __getitem__(self: Self, idx: int | slice | Sequence[int]) -> Any | Self:
def __getitem__(
self: Self, idx: int | slice | Sequence[int] | Series[Any]
) -> Any | Self:
if isinstance(idx, int):
return self._compliant_series[idx]
return self._from_compliant_series(self._compliant_series[idx])
Expand Down
18 changes: 18 additions & 0 deletions tests/series_only/__getitem___test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

import polars as pl
import pytest

import narwhals.stable.v1 as nw


def test_getitem() -> None:
spl = pl.Series([1, 2, 3])
assert spl[spl[0, 1]].equals(pl.Series([2, 3]))
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check what happens when there's missing values too please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using to_native() instead of to_numpy() allows the following tests to pass, but my complete lack of knowledge on numpy, narwhals, polars and related subjects makes me unsure they are correct and show expected behavior:

    spl = pl.Series([1, None, 3])
    assert spl[spl[0, 1]].equals(pl.Series([None, None]))

    snw = nw.from_native(spl, series_only=True)
    assert snw[snw[0, 1]].to_native().equals(pl.Series([None, None]))

What's your take on this?


snw = nw.from_native(spl, series_only=True)
assert snw[snw[0, 1]].to_native().equals(pl.Series([2, 3]))

spl = pl.Series([1, 2, 3])
snw = nw.from_native(spl, series_only=True)
assert pytest.raises(TypeError, lambda: snw[snw[True, False]])
Loading