Skip to content

Commit

Permalink
fix and test boolean Series indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 23, 2021
1 parent 7842413 commit ddd46fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions py-polars/polars/eager/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,18 @@ def __neg__(self) -> "Series":

def __getitem__(self, item: Any) -> Any:
if isinstance(item, int):
if item >= self.len():
raise IndexError
if item < 0:
item = self.len() + item
if self.dtype in (List, Date32, Date64, Object):
f = get_ffi_func("get_<>", self.dtype, self._s)
if f is None:
return NotImplemented
out = f(item)
if self.dtype == List:
return wrap_s(out)
return out

return self._s.get_idx(item)
# assume it is boolean mask
if isinstance(item, Series):
return Series._from_pyseries(self._s.filter(item._s))
Expand Down
4 changes: 4 additions & 0 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ impl PySeries {
}
}

pub fn get_idx(&self, py: Python, idx: usize) -> PyObject {
Wrap(self.series.get(idx)).into_py(py)
}

pub fn bitand(&self, other: &PySeries) -> PyResult<Self> {
let out = self.series.bitand(&other.series).map_err(PyPolarsEr::from)?;
Ok(out.into())
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ def test_rechunk():
assert a.n_chunks() == 1


def test_indexing():
a = pl.Series("a", [1, 2, None])
assert a[1] == 2
assert a[2] == None
b = pl.Series("b", [True, False])
assert b[0]
assert not b[1]
a = pl.Series("a", ["a", None])
assert a[0] == "a"
assert a[1] == None
a = pl.Series("a", [0.1, None])
assert a[0] == 0.1
assert a[1] == None


def test_arrow():
a = pl.Series("a", [1, 2, 3, None])
out = a.to_arrow()
Expand Down

0 comments on commit ddd46fe

Please sign in to comment.