Skip to content

Commit

Permalink
feat: Increase verbosity of duplicate column error message (#11899)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller authored Feb 13, 2024
1 parent 9370f19 commit fb51095
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,17 @@ pub(super) fn check_expand_literals(
all_equal_len = false;
}
let name = s.name();
polars_ensure!(names.insert(name), duplicate = name);

if !names.insert(name) {
let msg = format!(
"the name: '{}' is duplicate\n\n\
It's possible that multiple expressions are returning the same default column \
name. If this is the case, try renaming the columns with \
`.alias(\"new_name\")` to avoid duplicate column names.",
name
);
return Err(PolarsError::Duplicate(msg.into()));
}
}
}
// If all series are the same length it is ok. If not we can broadcast Series of length one.
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-plan/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,10 @@ impl LogicalPlanBuilder {

if !output_names.insert(field.name().clone()) {
let msg = format!(
"The name: '{}' passed to `LazyFrame.with_columns` is duplicate",
"the name: '{}' passed to `LazyFrame.with_columns` is duplicate\n\n\
It's possible that multiple expressions are returning the same default column name. \
If this is the case, try renaming the columns with `.alias(\"new_name\")` to avoid \
duplicate column names.",
field.name()
);
return raise_err!(polars_err!(ComputeError: msg), &self.0, into);
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def test_with_column_duplicates() -> None:
df = pl.DataFrame({"a": [0, None, 2, 3, None], "b": [None, 1, 2, 3, None]})
with pytest.raises(
pl.ComputeError,
match=r"The name: 'same' passed to `LazyFrame.with_columns` is duplicate",
match=r"the name: 'same' passed to `LazyFrame.with_columns` is duplicate.*",
):
assert df.with_columns([pl.all().alias("same")]).columns == ["a", "b", "same"]

Expand Down

0 comments on commit fb51095

Please sign in to comment.