Skip to content

Commit

Permalink
Merge pull request #192 from DeaMariaLeon/when
Browse files Browse the repository at this point in the history
Improve error message for is_in
  • Loading branch information
MarcoGorelli authored May 21, 2024
2 parents 325f7b6 + 9d85baa commit 8b1b678
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion narwhals/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,12 @@ def is_between(
)

def is_in(self, other: Any) -> Expr:
return self.__class__(lambda plx: self._call(plx).is_in(other))
if isinstance(other, Iterable) and not isinstance(other, (str, bytes)):
return self.__class__(lambda plx: self._call(plx).is_in(other))
else:
raise NotImplementedError(
"Narwhals `is_in` doesn't accept expressions as an argument, as opposed to Polars. You should provide an iterable instead."
)

def filter(self, other: Any) -> Expr:
return self.__class__(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ def test_is_in(df_raw: Any) -> None:
assert result[2]


@pytest.mark.parametrize("df_raw", [df_pandas, df_polars])
@pytest.mark.filterwarnings("ignore:np.find_common_type is deprecated:DeprecationWarning")
def test_is_in_other(df_raw: Any) -> None:
with pytest.raises(
NotImplementedError,
match=(
"Narwhals `is_in` doesn't accept expressions as an argument, as opposed to Polars. You should provide an iterable instead."
),
):
nw.from_native(df_raw).with_columns(contains=nw.col("c").is_in("sets"))


@pytest.mark.parametrize("df_raw", [df_pandas, df_polars])
@pytest.mark.filterwarnings("ignore:np.find_common_type is deprecated:DeprecationWarning")
def test_filter(df_raw: Any) -> None:
Expand Down

0 comments on commit 8b1b678

Please sign in to comment.