Skip to content

Commit

Permalink
Python rename drop_column(s) to drop (union eager/lazy) and pl.exclud…
Browse files Browse the repository at this point in the history
…e syntactic sugar
  • Loading branch information
ritchie46 committed Oct 1, 2021
1 parent 7100f53 commit 893be8d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
4 changes: 2 additions & 2 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -42,6 +42,7 @@ These functions can be used as expression and sometimes also in eager contexts.
concat_str
concat_list
when
exclude

Constructor
-----------
Expand Down
3 changes: 1 addition & 2 deletions py-polars/docs/source/reference/lazyframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ Manipulation/ selection
LazyFrame.join
LazyFrame.with_columns
LazyFrame.with_column
LazyFrame.drop_columns
LazyFrame.drop_column
LazyFrame.with_column_renamed
LazyFrame.drop
LazyFrame.rename
LazyFrame.reverse
LazyFrame.shift
Expand Down
21 changes: 7 additions & 14 deletions py-polars/polars/lazy/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,27 +548,20 @@ def with_column(self, expr: "Expr") -> "LazyFrame":
"""
return self.with_columns([expr])

def drop_columns(self, columns: tp.List[str]) -> "LazyFrame":
def drop(self, columns: Union[str, tp.List[str]]) -> "LazyFrame":
"""
Remove multiple columns from a DataFrame.
Remove one or multiple columns from a DataFrame.
Parameters
----------
columns
List of column names.
"""
return wrap_ldf(self._ldf.drop_columns(columns))
- Name of the column that should be removed.
- List of column names.
def drop_column(self, column: str) -> "LazyFrame":
"""
Remove a column from the DataFrame.
Parameters
----------
column
Name of the column that should be removed.
"""
return self.drop_columns([column])
if isinstance(columns, str):
columns = [columns]
return wrap_ldf(self._ldf.drop_columns(columns))

def with_column_renamed(self, existing_name: str, new_name: str) -> "LazyFrame":
"""
Expand Down
50 changes: 50 additions & 0 deletions py-polars/polars/lazy/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,56 @@ def any(name: Union[str, tp.List["pl.Expr"]]) -> "pl.Expr":
return col(name).sum() > 0


def exclude(columns: Union[str, tp.List[str]]) -> "pl.Expr":
"""
Exclude certain columns from a wildcard expression.
Syntactic sugar for:
>>> col("*").exclude()
Parameters
----------
columns
Column(s) to exclude from selection
Examples
--------
>>> df = pl.DataFrame({
>>> "a": [1, 2, 3],
>>> "b": ["a", "b", None],
>>> "c": [None, 2, 1]
>>> })
>>> df
shape: (3, 3)
╭─────┬──────┬──────╮
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞═════╪══════╪══════╡
│ 1 ┆ "a" ┆ null │
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 2 ┆ "b" ┆ 2 │
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 3 ┆ null ┆ 1 │
╰─────┴──────┴──────╯
>>> df.select(pl.exclude("b"))
shape: (3, 2)
╭─────┬──────╮
│ a ┆ c │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪══════╡
│ 1 ┆ null │
├╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 2 ┆ 2 │
├╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 3 ┆ 1 │
╰─────┴──────╯
"""
return col("*").exclude(columns)


def all(name: Optional[Union[str, tp.List["pl.Expr"]]] = None) -> "pl.Expr":
"""
This function is two things
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def test_rename():


def test_drop_columns():
out = pl.DataFrame({"a": [1], "b": [2], "c": [3]}).lazy().drop_columns(["a", "b"])
out = pl.DataFrame({"a": [1], "b": [2], "c": [3]}).lazy().drop(["a", "b"])
assert out.columns == ["c"]


Expand Down

0 comments on commit 893be8d

Please sign in to comment.