diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 738292bd54..076115bccf 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -10,7 +10,6 @@ from narwhals._pandas_like.series import PANDAS_TO_NUMPY_DTYPE_MISSING, PandasLikeSeries from narwhals._pandas_like.utils import ( align_and_extract_native, - check_column_names_are_unique, get_dtype_backend, native_to_narwhals_dtype, object_native_to_narwhals_dtype, @@ -22,6 +21,7 @@ Implementation, _into_arrow_table, _remap_full_join_keys, + check_column_names_are_unique, exclude_column_names, generate_temporary_column_name, parse_columns_to_drop, diff --git a/narwhals/_pandas_like/utils.py b/narwhals/_pandas_like/utils.py index 4ffeac468c..125dfa0e98 100644 --- a/narwhals/_pandas_like/utils.py +++ b/narwhals/_pandas_like/utils.py @@ -23,7 +23,7 @@ check_columns_exist, isinstance_or_issubclass, ) -from narwhals.exceptions import DuplicateError, ShapeError +from narwhals.exceptions import ShapeError if TYPE_CHECKING: from pandas._typing import Dtype as PandasDtype @@ -592,25 +592,6 @@ def select_columns_by_name( raise -def check_column_names_are_unique(columns: pd.Index[str]) -> None: - try: - len_unique_columns = len(columns.drop_duplicates()) - except Exception: # noqa: BLE001 # pragma: no cover - msg = f"Expected hashable (e.g. str or int) column names, got: {columns}" - raise ValueError(msg) from None - - if len(columns) != len_unique_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 DuplicateError(msg) - - def is_non_nullable_boolean(s: PandasLikeSeries) -> bool: # cuDF booleans are nullable but the native dtype is still 'bool'. return ( diff --git a/narwhals/_utils.py b/narwhals/_utils.py index b3eb0f0fd6..fba86db337 100644 --- a/narwhals/_utils.py +++ b/narwhals/_utils.py @@ -1573,7 +1573,12 @@ def check_columns_exist( def check_column_names_are_unique(columns: Collection[str]) -> None: - len_unique_columns = len(set(columns)) + try: + len_unique_columns = len(set(columns)) + except TypeError as exc: # pragma: no cover + msg = f"Expected hashable (e.g. str or int) column names, got: {columns}" + raise TypeError(msg) from exc + if len(columns) != len_unique_columns: from collections import Counter