| 
8 | 8 | import pandas as pd  | 
9 | 9 | 
 
  | 
10 | 10 | from narwhals._compliant.series import EagerSeriesNamespace  | 
11 |  | -from narwhals.exceptions import ColumnNotFoundError, DuplicateError, ShapeError  | 
 | 11 | +from narwhals.exceptions import DuplicateError, ShapeError  | 
12 | 12 | from narwhals.utils import (  | 
13 | 13 |     Implementation,  | 
14 | 14 |     Version,  | 
15 | 15 |     _DeferredIterable,  | 
 | 16 | +    check_columns_exist,  | 
16 | 17 |     isinstance_or_issubclass,  | 
17 | 18 | )  | 
18 | 19 | 
 
  | 
@@ -622,21 +623,21 @@ def select_columns_by_name(  | 
622 | 623 |     ):  | 
623 | 624 |         # See https://github.com/narwhals-dev/narwhals/issues/1349#issuecomment-2470118122  | 
624 | 625 |         # for why we need this  | 
625 |  | -        available_columns = df.columns.tolist()  # type: ignore[attr-defined]  | 
626 |  | -        missing_columns = [x for x in column_names if x not in available_columns]  | 
627 |  | -        if missing_columns:  # pragma: no cover  | 
628 |  | -            raise ColumnNotFoundError.from_missing_and_available_column_names(  | 
629 |  | -                missing_columns, available_columns  | 
630 |  | -            )  | 
 | 626 | +        if error := check_columns_exist(  | 
 | 627 | +            column_names,  # type: ignore[arg-type]  | 
 | 628 | +            available=df.columns.tolist(),  # type: ignore[attr-defined]  | 
 | 629 | +        ):  | 
 | 630 | +            raise error  | 
631 | 631 |         return df.loc[:, column_names]  # type: ignore[attr-defined]  | 
632 | 632 |     try:  | 
633 | 633 |         return df[column_names]  # type: ignore[index]  | 
634 | 634 |     except KeyError as e:  | 
635 |  | -        available_columns = df.columns.tolist()  # type: ignore[attr-defined]  | 
636 |  | -        missing_columns = [x for x in column_names if x not in available_columns]  | 
637 |  | -        raise ColumnNotFoundError.from_missing_and_available_column_names(  | 
638 |  | -            missing_columns, available_columns  | 
639 |  | -        ) from e  | 
 | 635 | +        if error := check_columns_exist(  | 
 | 636 | +            column_names,  # type: ignore[arg-type]  | 
 | 637 | +            available=df.columns.tolist(),  # type: ignore[attr-defined]  | 
 | 638 | +        ):  | 
 | 639 | +            raise error from e  | 
 | 640 | +        raise  | 
640 | 641 | 
 
  | 
641 | 642 | 
 
  | 
642 | 643 | def check_column_names_are_unique(columns: pd.Index[str]) -> None:  | 
 | 
0 commit comments