@@ -363,6 +363,44 @@ def _from_series(cls, series: EagerSeriesT) -> Self:
363363 version = series ._version ,
364364 )
365365
366+ def _with_alias_output_names (self , alias_name : AliasName | None , / ) -> Self :
367+ current_alias_output_names = self ._alias_output_names
368+ alias_output_names : AliasNames | None = (
369+ None
370+ if alias_name is None
371+ else (
372+ lambda output_names : [
373+ alias_name (x ) for x in current_alias_output_names (output_names )
374+ ]
375+ )
376+ if current_alias_output_names is not None
377+ else (lambda output_names : [alias_name (x ) for x in output_names ])
378+ )
379+
380+ def func (df : EagerDataFrameT ) -> list [EagerSeriesT ]:
381+ if alias_output_names :
382+ return [
383+ series .alias (name )
384+ for series , name in zip (
385+ self (df ), alias_output_names (self ._evaluate_output_names (df ))
386+ )
387+ ]
388+ return [
389+ series .alias (name )
390+ for series , name in zip (self (df ), self ._evaluate_output_names (df ))
391+ ]
392+
393+ return self .__class__ (
394+ func ,
395+ depth = self ._depth ,
396+ function_name = self ._function_name ,
397+ evaluate_output_names = self ._evaluate_output_names ,
398+ alias_output_names = alias_output_names ,
399+ implementation = self ._implementation ,
400+ version = self ._version ,
401+ scalar_kwargs = self ._scalar_kwargs ,
402+ )
403+
366404 def _reuse_series (
367405 self ,
368406 method_name : str ,
@@ -1035,7 +1073,7 @@ class CompliantExprNameNamespace( # type: ignore[misc]
10351073 Protocol [CompliantExprT_co ],
10361074):
10371075 def keep (self ) -> CompliantExprT_co :
1038- return self ._from_callable (lambda name : name , alias = False )
1076+ return self ._from_callable (None )
10391077
10401078 def map (self , function : AliasName ) -> CompliantExprT_co :
10411079 return self ._from_callable (function )
@@ -1059,41 +1097,27 @@ def fn(output_names: Sequence[str], /) -> Sequence[str]:
10591097
10601098 return fn
10611099
1062- def _from_callable (
1063- self , func : AliasName , / , * , alias : bool = True
1064- ) -> CompliantExprT_co : ...
1100+ def _from_callable (self , func : AliasName | None , / ) -> CompliantExprT_co : ...
10651101
10661102
10671103class EagerExprNameNamespace (
10681104 EagerExprNamespace [EagerExprT ],
10691105 CompliantExprNameNamespace [EagerExprT ],
10701106 Generic [EagerExprT ],
10711107):
1072- def _from_callable (self , func : AliasName , / , * , alias : bool = True ) -> EagerExprT :
1108+ def _from_callable (self , func : AliasName | None ) -> EagerExprT :
10731109 expr = self .compliant
1074- return type (expr )(
1075- lambda df : [
1076- series .alias (func (name ))
1077- for series , name in zip (expr (df ), expr ._evaluate_output_names (df ))
1078- ],
1079- depth = expr ._depth ,
1080- function_name = expr ._function_name ,
1081- evaluate_output_names = expr ._evaluate_output_names ,
1082- alias_output_names = self ._alias_output_names (func ) if alias else None ,
1083- implementation = expr ._implementation ,
1084- version = expr ._version ,
1085- scalar_kwargs = expr ._scalar_kwargs ,
1086- )
1110+ return expr ._with_alias_output_names (func )
10871111
10881112
10891113class LazyExprNameNamespace (
10901114 LazyExprNamespace [LazyExprT ],
10911115 CompliantExprNameNamespace [LazyExprT ],
10921116 Generic [LazyExprT ],
10931117):
1094- def _from_callable (self , func : AliasName , / , * , alias : bool = True ) -> LazyExprT :
1118+ def _from_callable (self , func : AliasName | None ) -> LazyExprT :
10951119 expr = self .compliant
1096- output_names = self ._alias_output_names (func ) if alias else None
1120+ output_names = self ._alias_output_names (func ) if func else None
10971121 return expr ._with_alias_output_names (output_names )
10981122
10991123
0 commit comments