Skip to content

Commit 2777822

Browse files
committed
feat: adds EagerExprNameNamespace
Didn't realise until now that both backends did the same thing
1 parent d53ce01 commit 2777822

File tree

6 files changed

+74
-184
lines changed

6 files changed

+74
-184
lines changed

narwhals/_arrow/expr.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import pyarrow.compute as pc
1010

11-
from narwhals._arrow.expr_name import ArrowExprNameNamespace
1211
from narwhals._arrow.series import ArrowSeries
1312
from narwhals._compliant import EagerExpr
1413
from narwhals._expression_parsing import ExprKind
@@ -263,7 +262,3 @@ def rank(
263262
return self._reuse_series("rank", method=method, descending=descending)
264263

265264
ewm_mean = not_implemented()
266-
267-
@property
268-
def name(self: Self) -> ArrowExprNameNamespace:
269-
return ArrowExprNameNamespace(self)

narwhals/_arrow/expr_name.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

narwhals/_arrow/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pyarrow as pa
1313
import pyarrow.compute as pc
1414

15-
from narwhals.utils import _ExprNamespace
1615
from narwhals.utils import _SeriesNamespace
1716
from narwhals.utils import import_dtypes_module
1817
from narwhals.utils import isinstance_or_issubclass
@@ -24,7 +23,6 @@
2423
from typing_extensions import TypeAlias
2524
from typing_extensions import TypeIs
2625

27-
from narwhals._arrow.expr import ArrowExpr
2826
from narwhals._arrow.series import ArrowSeries
2927
from narwhals._arrow.typing import ArrowArray
3028
from narwhals._arrow.typing import ArrowChunkedArray
@@ -548,8 +546,3 @@ def pad_series(
548546
class ArrowSeriesNamespace(_SeriesNamespace["ArrowSeries", "ArrowChunkedArray"]):
549547
def __init__(self: Self, series: ArrowSeries, /) -> None:
550548
self._compliant_series = series
551-
552-
553-
class ArrowExprNamespace(_ExprNamespace["ArrowExpr"]):
554-
def __init__(self: Self, expr: ArrowExpr, /) -> None:
555-
self._compliant_expr = expr

narwhals/_compliant/expr.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from narwhals._compliant.any_namespace import CatNamespace
1616
from narwhals._compliant.any_namespace import DateTimeNamespace
1717
from narwhals._compliant.any_namespace import ListNamespace
18+
from narwhals._compliant.any_namespace import NameNamespace
1819
from narwhals._compliant.any_namespace import StringNamespace
1920
from narwhals._compliant.namespace import CompliantNamespace
2021
from narwhals._compliant.typing import CompliantFrameT
@@ -46,7 +47,6 @@
4647

4748
from typing_extensions import Self
4849

49-
from narwhals._compliant.any_namespace import NameNamespace
5050
from narwhals._compliant.namespace import CompliantNamespace
5151
from narwhals._compliant.namespace import EagerNamespace
5252
from narwhals._compliant.series import CompliantSeries
@@ -783,7 +783,8 @@ def str(self) -> EagerExprStringNamespace[Self]:
783783
return EagerExprStringNamespace(self)
784784

785785
@property
786-
def name(self) -> NameNamespace[Self]: ...
786+
def name(self) -> EagerExprNameNamespace[Self]:
787+
return EagerExprNameNamespace(self)
787788

788789

789790
# NOTE: See (https://github.com/narwhals-dev/narwhals/issues/2044#issuecomment-2674262833)
@@ -903,6 +904,77 @@ def len(self) -> EagerExprT:
903904
return self.compliant._reuse_series_namespace("list", "len")
904905

905906

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+
906978
class EagerExprStringNamespace(
907979
EagerExprNamespace[EagerExprT], StringNamespace[EagerExprT], Generic[EagerExprT]
908980
):

narwhals/_pandas_like/expr.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from narwhals._expression_parsing import ExprKind
1212
from narwhals._expression_parsing import evaluate_output_names_and_aliases
1313
from narwhals._expression_parsing import is_elementary_expression
14-
from narwhals._pandas_like.expr_name import PandasLikeExprNameNamespace
1514
from narwhals._pandas_like.group_by import AGGREGATIONS_TO_PANDAS_EQUIVALENT
1615
from narwhals._pandas_like.series import PandasLikeSeries
1716
from narwhals.dependencies import get_numpy
@@ -341,7 +340,3 @@ def rank(
341340
return self._reuse_series(
342341
"rank", call_kwargs={"method": method, "descending": descending}
343342
)
344-
345-
@property
346-
def name(self: Self) -> PandasLikeExprNameNamespace:
347-
return PandasLikeExprNameNamespace(self)

narwhals/_pandas_like/expr_name.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)