|
15 | 15 | from narwhals._compliant.any_namespace import CatNamespace |
16 | 16 | from narwhals._compliant.any_namespace import DateTimeNamespace |
17 | 17 | from narwhals._compliant.any_namespace import ListNamespace |
| 18 | +from narwhals._compliant.any_namespace import NameNamespace |
18 | 19 | from narwhals._compliant.any_namespace import StringNamespace |
19 | 20 | from narwhals._compliant.namespace import CompliantNamespace |
20 | 21 | from narwhals._compliant.typing import CompliantFrameT |
|
46 | 47 |
|
47 | 48 | from typing_extensions import Self |
48 | 49 |
|
49 | | - from narwhals._compliant.any_namespace import NameNamespace |
50 | 50 | from narwhals._compliant.namespace import CompliantNamespace |
51 | 51 | from narwhals._compliant.namespace import EagerNamespace |
52 | 52 | from narwhals._compliant.series import CompliantSeries |
@@ -783,7 +783,8 @@ def str(self) -> EagerExprStringNamespace[Self]: |
783 | 783 | return EagerExprStringNamespace(self) |
784 | 784 |
|
785 | 785 | @property |
786 | | - def name(self) -> NameNamespace[Self]: ... |
| 786 | + def name(self) -> EagerExprNameNamespace[Self]: |
| 787 | + return EagerExprNameNamespace(self) |
787 | 788 |
|
788 | 789 |
|
789 | 790 | # NOTE: See (https://github.com/narwhals-dev/narwhals/issues/2044#issuecomment-2674262833) |
@@ -903,6 +904,77 @@ def len(self) -> EagerExprT: |
903 | 904 | return self.compliant._reuse_series_namespace("list", "len") |
904 | 905 |
|
905 | 906 |
|
| 907 | +class EagerExprNameNamespace( |
| 908 | + EagerExprNamespace[EagerExprT], NameNamespace[EagerExprT], Generic[EagerExprT] |
| 909 | +): |
| 910 | + def keep(self) -> EagerExprT: |
| 911 | + return self._from_colname_func_and_alias_output_names( |
| 912 | + name_mapping_func=lambda name: name |
| 913 | + ) |
| 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 | + ) |
| 922 | + |
| 923 | + 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 | + ) |
| 930 | + |
| 931 | + 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 | + ) |
| 938 | + |
| 939 | + 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 | + ) |
| 946 | + |
| 947 | + 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 | + ) |
| 954 | + |
| 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 | + ) |
| 966 | + ], |
| 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, |
| 975 | + ) |
| 976 | + |
| 977 | + |
906 | 978 | class EagerExprStringNamespace( |
907 | 979 | EagerExprNamespace[EagerExprT], StringNamespace[EagerExprT], Generic[EagerExprT] |
908 | 980 | ): |
|
0 commit comments