Skip to content

Commit

Permalink
fix[python]: ensure is_in empty list has dtype null (#4562)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 25, 2022
1 parent 93e3dc1 commit 3b7e1be
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
7 changes: 5 additions & 2 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3269,7 +3269,7 @@ def pow(self, exponent: int | float | pli.Series | Expr) -> Expr:
exponent = expr_to_lit_or_expr(exponent)
return wrap_expr(self._pyexpr.pow(exponent._pyexpr))

def is_in(self, other: Expr | Sequence[Any] | str) -> Expr:
def is_in(self, other: Expr | Sequence[Any] | str | pli.Series) -> Expr:
"""
Check if elements of this expression are present in the other Series.
Expand Down Expand Up @@ -3303,7 +3303,10 @@ def is_in(self, other: Expr | Sequence[Any] | str) -> Expr:
"""
if isinstance(other, Sequence) and not isinstance(other, str):
other = pli.lit(pli.Series(other))
if len(other) == 0:
other = pli.lit(None)
else:
other = pli.lit(pli.Series(other))
else:
other = expr_to_lit_or_expr(other, str_to_lit=False)
return wrap_expr(self._pyexpr.is_in(other._pyexpr))
Expand Down
8 changes: 2 additions & 6 deletions py-polars/polars/internals/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ def is_not_nan(self) -> Series:
"""
return wrap_s(self._s.is_not_nan())

def is_in(self, other: Series | Sequence[object]) -> Series:
def is_in(self, other: Series | Sequence[Any]) -> Series:
"""
Check if elements of this Series are in the other Series, or
if this Series is itself a member of the other Series.
Expand Down Expand Up @@ -1963,11 +1963,7 @@ def is_in(self, other: Series | Sequence[object]) -> Series:
]
"""
if isinstance(other, str):
raise TypeError("'other' parameter expects non-string sequence data")
elif isinstance(other, Sequence):
other = Series("", other)
return wrap_s(self._s.is_in(other._s))
return self.to_frame().select(pli.col(self.name).is_in(other)).to_series()

def arg_true(self) -> Series:
"""
Expand Down
11 changes: 0 additions & 11 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,17 +892,6 @@ impl PySeries {
})
}

#[cfg(feature = "is_in")]
pub fn is_in(&self, py: Python, other: &PySeries) -> PyResult<Self> {
py.allow_threads(|| {
let out = self
.series
.is_in(&other.series)
.map_err(PyPolarsErr::from)?;
Ok(out.into_series().into())
})
}

pub fn clone(&self) -> Self {
PySeries::new(self.series.clone())
}
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,7 @@ def test_list_eval_type_coercion() -> None:
.alias("col_last")
]
).to_dict(False) == {"col_last": [[3]]}


def test_is_empty_list_4559() -> None:
assert pl.Series(["a"]).is_in([]).to_list() == [False]

0 comments on commit 3b7e1be

Please sign in to comment.