Skip to content

Commit

Permalink
with_columns update on duplicates (#4179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 29, 2022
1 parent f5617af commit 56cc086
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions polars/polars-lazy/src/physical_plan/executors/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ impl Executor for StackExec {
state.clear_expr_cache();

let schema = &*self.input_schema;
for s in res {
df.with_column_and_schema(s, schema)?;
for (i, s) in res.into_iter().enumerate() {
// we need to branch here
// because users can add multiple columns with the same name
if i == 0 || schema.get(s.name()).is_some() {
df.with_column_and_schema(s, schema)?;
} else {
df.with_column(s.clone())?;
}
}

Ok(df)
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ def test_fill_null_minimal_upcast_4056() -> None:
df = df.with_columns(pl.col("a").cast(pl.Int8))
assert df.with_column(pl.col(pl.Int8).fill_null(-1)).dtypes[0] == pl.Int8
assert df.with_column(pl.col(pl.Int8).fill_null(-1000)).dtypes[0] == pl.Int32


def test_with_column_duplicates() -> None:
df = pl.DataFrame({"a": [0, None, 2, 3, None], "b": [None, 1, 2, 3, None]})
assert df.with_columns([pl.all().alias("same")]).columns == ["a", "b", "same"]

0 comments on commit 56cc086

Please sign in to comment.