Skip to content
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

feat: Increase verbosity of duplicate column error message #11899

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading