|
18 | 18 | from narwhals._compliant.any_namespace import NameNamespace |
19 | 19 | from narwhals._compliant.any_namespace import StringNamespace |
20 | 20 | from narwhals._compliant.namespace import CompliantNamespace |
| 21 | +from narwhals._compliant.typing import AliasName |
| 22 | +from narwhals._compliant.typing import AliasNames |
21 | 23 | from narwhals._compliant.typing import CompliantFrameT |
22 | 24 | from narwhals._compliant.typing import CompliantLazyFrameT |
23 | 25 | from narwhals._compliant.typing import CompliantSeriesOrNativeExprT_co |
@@ -908,70 +910,45 @@ class EagerExprNameNamespace( |
908 | 910 | EagerExprNamespace[EagerExprT], NameNamespace[EagerExprT], Generic[EagerExprT] |
909 | 911 | ): |
910 | 912 | def keep(self) -> EagerExprT: |
911 | | - return self._from_colname_func_and_alias_output_names( |
912 | | - name_mapping_func=lambda name: name |
913 | | - ) |
| 913 | + return self._from_callable(lambda name: name, alias=False) |
914 | 914 |
|
915 | | - def map(self, function: Callable[[str], str]) -> EagerExprT: |
916 | | - return self._from_colname_func_and_alias_output_names( |
917 | | - name_mapping_func=function, |
918 | | - alias_output_names=lambda output_names: [ |
919 | | - function(name) for name in output_names |
920 | | - ], |
921 | | - ) |
| 915 | + def map(self, function: AliasName) -> EagerExprT: |
| 916 | + return self._from_callable(function) |
922 | 917 |
|
923 | 918 | def prefix(self, prefix: str) -> EagerExprT: |
924 | | - return self._from_colname_func_and_alias_output_names( |
925 | | - name_mapping_func=lambda name: f"{prefix}{name}", |
926 | | - alias_output_names=lambda output_names: [ |
927 | | - f"{prefix}{output_name}" for output_name in output_names |
928 | | - ], |
929 | | - ) |
| 919 | + return self._from_callable(lambda name: f"{prefix}{name}") |
930 | 920 |
|
931 | 921 | def suffix(self, suffix: str) -> EagerExprT: |
932 | | - return self._from_colname_func_and_alias_output_names( |
933 | | - name_mapping_func=lambda name: f"{name}{suffix}", |
934 | | - alias_output_names=lambda output_names: [ |
935 | | - f"{output_name}{suffix}" for output_name in output_names |
936 | | - ], |
937 | | - ) |
| 922 | + return self._from_callable(lambda name: f"{name}{suffix}") |
938 | 923 |
|
939 | 924 | def to_lowercase(self) -> EagerExprT: |
940 | | - return self._from_colname_func_and_alias_output_names( |
941 | | - name_mapping_func=str.lower, |
942 | | - alias_output_names=lambda output_names: [ |
943 | | - name.lower() for name in output_names |
944 | | - ], |
945 | | - ) |
| 925 | + return self._from_callable(str.lower) |
946 | 926 |
|
947 | 927 | def to_uppercase(self) -> EagerExprT: |
948 | | - return self._from_colname_func_and_alias_output_names( |
949 | | - name_mapping_func=str.upper, |
950 | | - alias_output_names=lambda output_names: [ |
951 | | - name.upper() for name in output_names |
952 | | - ], |
953 | | - ) |
| 928 | + return self._from_callable(str.upper) |
954 | 929 |
|
955 | | - def _from_colname_func_and_alias_output_names( |
956 | | - self, |
957 | | - name_mapping_func: Callable[[str], str], |
958 | | - alias_output_names: Callable[[Sequence[str]], Sequence[str]] | None = None, |
959 | | - ) -> EagerExprT: |
960 | | - return type(self.compliant)( |
961 | | - call=lambda df: [ |
962 | | - series.alias(name_mapping_func(name)) |
963 | | - for series, name in zip( |
964 | | - self.compliant._call(df), self.compliant._evaluate_output_names(df) |
965 | | - ) |
| 930 | + @staticmethod |
| 931 | + def _alias_output_names(func: AliasName, /) -> AliasNames: |
| 932 | + def fn(output_names: Sequence[str], /) -> Sequence[str]: |
| 933 | + return [func(name) for name in output_names] |
| 934 | + |
| 935 | + return fn |
| 936 | + |
| 937 | + def _from_callable(self, func: AliasName, /, *, alias: bool = True) -> EagerExprT: |
| 938 | + expr = self.compliant |
| 939 | + return type(expr)( |
| 940 | + lambda df: [ |
| 941 | + series.alias(func(name)) |
| 942 | + for series, name in zip(expr(df), expr._evaluate_output_names(df)) |
966 | 943 | ], |
967 | | - depth=self.compliant._depth, |
968 | | - function_name=self.compliant._function_name, |
969 | | - evaluate_output_names=self.compliant._evaluate_output_names, |
970 | | - alias_output_names=alias_output_names, |
971 | | - backend_version=self.compliant._backend_version, |
972 | | - implementation=self.compliant._implementation, |
973 | | - version=self.compliant._version, |
974 | | - call_kwargs=self.compliant._call_kwargs, |
| 944 | + depth=expr._depth, |
| 945 | + function_name=expr._function_name, |
| 946 | + evaluate_output_names=expr._evaluate_output_names, |
| 947 | + alias_output_names=self._alias_output_names(func) if alias else None, |
| 948 | + backend_version=expr._backend_version, |
| 949 | + implementation=expr._implementation, |
| 950 | + version=expr._version, |
| 951 | + call_kwargs=expr._call_kwargs, |
975 | 952 | ) |
976 | 953 |
|
977 | 954 |
|
|
0 commit comments