-
Notifications
You must be signed in to change notification settings - Fork 133
feat: add with_columns
#909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
35656f1
b2fc588
7228ca8
428b209
26405c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, List, TYPE_CHECKING | ||
| from typing import Any, Iterable, List, TYPE_CHECKING | ||
| from datafusion.record_batch import RecordBatchStream | ||
| from typing_extensions import deprecated | ||
| from datafusion.plan import LogicalPlan, ExecutionPlan | ||
|
|
@@ -160,6 +160,53 @@ def with_column(self, name: str, expr: Expr) -> DataFrame: | |
| """ | ||
| return DataFrame(self.df.with_column(name, expr.expr)) | ||
|
|
||
| def with_columns( | ||
| self, *exprs: Expr | Iterable[Expr], **named_exprs: Expr | ||
| ) -> DataFrame: | ||
| """Add columns to the DataFrame. | ||
|
|
||
| By passing expressions, iteratables of expressions, or named expressions. To | ||
| pass named expressions use the form name=Expr. | ||
|
|
||
| Example usage: | ||
|
|
||
| The following will add 4 columns labeled a, b, c, and d. | ||
|
|
||
| df = df.with_columns( | ||
| lit(0).alias('a'), | ||
| [lit(1).alias('b'), lit(2).alias('c')], | ||
| d=lit(3) | ||
| ) | ||
|
||
|
|
||
| Args: | ||
| *exprs: Name of the column to add. | ||
| **named_exprs: Expression to compute the column. | ||
|
|
||
| Returns: | ||
| DataFrame with the new column. | ||
| """ | ||
timsaucer marked this conversation as resolved.
Show resolved
Hide resolved
timsaucer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _simplify_expression( | ||
| *exprs: Expr | Iterable[Expr], **named_exprs: Expr | ||
| ) -> list[Expr]: | ||
| expr_list = [] | ||
| for expr in exprs: | ||
| if isinstance(expr, Expr): | ||
| expr_list.append(expr.expr) | ||
| elif isinstance(expr, Iterable): | ||
| for inner_expr in expr: | ||
| expr_list.append(inner_expr.expr) | ||
| else: | ||
| raise NotImplementedError | ||
| if named_exprs: | ||
| for alias, expr in named_exprs.items(): | ||
| expr_list.append(expr.alias(alias).expr) | ||
| return expr_list | ||
|
|
||
| expressions = _simplify_expression(*exprs, **named_exprs) | ||
|
|
||
| return DataFrame(self.df.with_columns(expressions)) | ||
|
|
||
| def with_column_renamed(self, old_name: str, new_name: str) -> DataFrame: | ||
| r"""Rename one column by applying a new projection. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.