Skip to content

Commit

Permalink
feat: better error message for duplicate column names in pandas (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Oct 28, 2024
1 parent 4c77fa5 commit cd781db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 8 additions & 1 deletion narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ def _validate_columns(self, columns: pd.Index) -> None:
raise ValueError(msg) from None

if len(columns) != len_unique_columns:
msg = f"Expected unique column names, got: {columns}"
from collections import Counter

counter = Counter(columns)
msg = ""
for key, value in counter.items():
if value > 1:
msg += f"\n- '{key}' {value} times"
msg = f"Expected unique column names, got:{msg}"
raise ValueError(msg)

def _from_native_frame(self, df: Any) -> Self:
Expand Down
4 changes: 3 additions & 1 deletion tests/translate/from_native_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def test_pandas_like_validate() -> None:
df2 = pd.DataFrame({"b": [1, 2, 3]})
df = pd.concat([df1, df2, df2], axis=1)

with pytest.raises(ValueError, match="Expected unique column names"):
with pytest.raises(
ValueError, match=r"Expected unique column names, got:\n- 'b' 2 times"
):
nw.from_native(df)


Expand Down

0 comments on commit cd781db

Please sign in to comment.