Skip to content

Commit

Permalink
python: drop_nulls expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 5, 2021
1 parent 253ca62 commit a5d8ed6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Manipulation/ selection
Expr.head
Expr.tail
Expr.reinterpret
Expr.drop_nulls

Column names
------------
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def __getitem__(self, item: Any) -> Any:
if isinstance(item, slice):
# special case df[::-1]
if item.start is None and item.stop is None and item.step == -1:
return self.select(pl.col("*").reverse())
return self.select(pl.col("*").reverse()) # type: ignore

if getattr(item, "end", False):
raise ValueError("A slice with steps larger than 1 is not supported.")
Expand All @@ -954,7 +954,7 @@ def __getitem__(self, item: Any) -> Any:
else:
# df[start:stop:step]
return self.select(
pl.col("*").slice(start, length).take_every(item.step)
pl.col("*").slice(start, length).take_every(item.step) # type: ignore
)

# select multiple columns
Expand Down
24 changes: 16 additions & 8 deletions py-polars/polars/lazy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ def slice(self, offset: int, length: int) -> "Expr":
"""
return wrap_expr(self._pyexpr.slice(offset, length))

def drop_nulls(self) -> "Expr":
"""
Syntactic sugar for:
>>> col("foo").filter(col("foo").is_not_null())
"""
return self.filter(self.is_not_null())

def cum_sum(self, reverse: bool = False) -> "Expr":
"""
Get an array with the cumulative sum computed at every element.
Expand Down Expand Up @@ -664,14 +672,14 @@ def over(self, expr: Union[str, "Expr", tp.List["Expr"]]) -> "Expr":
--------
>>> df = DataFrame({
"groups": [1, 1, 2, 2, 1, 2, 3, 3, 1],
"values": [1, 2, 3, 4, 5, 6, 7, 8, 8]
})
>>> print(df.lazy()
.select([
col("groups")
sum("values").over("groups"))
]).collect())
>>> "groups": [1, 1, 2, 2, 1, 2, 3, 3, 1],
>>> "values": [1, 2, 3, 4, 5, 6, 7, 8, 8]
>>>})
>>> (df.lazy()
>>> .select([
>>> col("groups")
>>> sum("values").over("groups"))
>>> ]).collect())
╭────────┬────────╮
│ groups ┆ values │
│ --- ┆ --- │
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,8 @@ def test_head_groupby():
assert out.frame_equal(
pl.DataFrame({"str": ["a", "a", "b", "c", "c"], "nrs": [3, 5, 6, 1, 2]})
)


def test_drop_nulls():
df = pl.DataFrame({"nrs": [1, 2, 3, 4, 5, None]})
assert df.select(col("nrs").drop_nulls()).shape == (5, 1)

0 comments on commit a5d8ed6

Please sign in to comment.