Skip to content

Commit

Permalink
feat: Field expansion renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 22, 2024
1 parent 215528e commit 0b12fdd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions crates/polars-plan/src/logical_plan/conversion/expr_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,19 @@ fn expand_struct_fields(
polars_ensure!(name.as_ref() != "*", InvalidOperation: "cannot combine wildcards and column names");

if !exclude.contains(name) {
result.push(replace_struct_multiple_fields_with_field(
full_expr.clone(),
name,
))
let mut new_expr = replace_struct_multiple_fields_with_field(full_expr.clone(), name);
match new_expr {
Expr::KeepName(expr) => {
new_expr = Expr::Alias(expr, name.clone());
},
Expr::RenameAlias { expr, function } => {
let name = function.call(&name)?;
new_expr = Expr::Alias(expr, ColumnName::from(name));
},
_ => {},
}

result.push(new_expr)
}
}
Ok(())
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/test_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ def test_struct_field_expand_star() -> None:
)
struct_df = df.select(pl.struct(["aaa", "bbb", "ccc", "ddd"]).alias("struct_col"))
assert_frame_equal(struct_df.select(pl.col("struct_col").struct.field("*")), df)


def test_struct_field_expand_rewrite() -> None:
df = pl.DataFrame({"A": [1], "B": [2]})
assert df.select(
pl.struct(["A", "B"]).struct.field("*").name.prefix("foo_")
).to_dict(as_series=False) == {"foo_A": [1], "foo_B": [2]}

0 comments on commit 0b12fdd

Please sign in to comment.