|
21 | 21 |
|
22 | 22 | from __future__ import annotations
|
23 | 23 |
|
24 |
| -from typing import Any, List, TYPE_CHECKING, Literal |
| 24 | +from typing import Any, Iterable, List, Literal, TYPE_CHECKING |
25 | 25 | from datafusion.record_batch import RecordBatchStream
|
26 | 26 | from typing_extensions import deprecated
|
27 | 27 | from datafusion.plan import LogicalPlan, ExecutionPlan
|
@@ -171,6 +171,51 @@ def with_column(self, name: str, expr: Expr) -> DataFrame:
|
171 | 171 | """
|
172 | 172 | return DataFrame(self.df.with_column(name, expr.expr))
|
173 | 173 |
|
| 174 | + def with_columns( |
| 175 | + self, *exprs: Expr | Iterable[Expr], **named_exprs: Expr |
| 176 | + ) -> DataFrame: |
| 177 | + """Add columns to the DataFrame. |
| 178 | +
|
| 179 | + By passing expressions, iteratables of expressions, or named expressions. To |
| 180 | + pass named expressions use the form name=Expr. |
| 181 | +
|
| 182 | + Example usage: The following will add 4 columns labeled a, b, c, and d:: |
| 183 | +
|
| 184 | + df = df.with_columns( |
| 185 | + lit(0).alias('a'), |
| 186 | + [lit(1).alias('b'), lit(2).alias('c')], |
| 187 | + d=lit(3) |
| 188 | + ) |
| 189 | +
|
| 190 | + Args: |
| 191 | + exprs: Either a single expression or an iterable of expressions to add. |
| 192 | + named_exprs: Named expressions in the form of ``name=expr`` |
| 193 | +
|
| 194 | + Returns: |
| 195 | + DataFrame with the new columns added. |
| 196 | + """ |
| 197 | + |
| 198 | + def _simplify_expression( |
| 199 | + *exprs: Expr | Iterable[Expr], **named_exprs: Expr |
| 200 | + ) -> list[Expr]: |
| 201 | + expr_list = [] |
| 202 | + for expr in exprs: |
| 203 | + if isinstance(expr, Expr): |
| 204 | + expr_list.append(expr.expr) |
| 205 | + elif isinstance(expr, Iterable): |
| 206 | + for inner_expr in expr: |
| 207 | + expr_list.append(inner_expr.expr) |
| 208 | + else: |
| 209 | + raise NotImplementedError |
| 210 | + if named_exprs: |
| 211 | + for alias, expr in named_exprs.items(): |
| 212 | + expr_list.append(expr.alias(alias).expr) |
| 213 | + return expr_list |
| 214 | + |
| 215 | + expressions = _simplify_expression(*exprs, **named_exprs) |
| 216 | + |
| 217 | + return DataFrame(self.df.with_columns(expressions)) |
| 218 | + |
174 | 219 | def with_column_renamed(self, old_name: str, new_name: str) -> DataFrame:
|
175 | 220 | r"""Rename one column by applying a new projection.
|
176 | 221 |
|
|
0 commit comments