Skip to content

Improve error message about duplicate columns in df.explode #49264

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

Merged
merged 3 commits into from
Oct 26, 2022
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
6 changes: 5 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8939,7 +8939,11 @@ def explode(
3 4 1 e
"""
if not self.columns.is_unique:
raise ValueError("columns must be unique")
duplicate_cols = self.columns[self.columns.duplicated()].tolist()
raise ValueError(
"DataFrame columns must be unique. "
+ f"Duplicate columns: {duplicate_cols}"
)

columns: list[Hashable]
if is_scalar(column) or isinstance(column, tuple):
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/frame/methods/test_explode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

Expand All @@ -18,7 +20,10 @@ def test_error():
df.explode(list("AA"))

df.columns = list("AA")
with pytest.raises(ValueError, match="columns must be unique"):
with pytest.raises(
ValueError,
match=re.escape("DataFrame columns must be unique. Duplicate columns: ['A']"),
):
df.explode("A")


Expand Down