Skip to content

Commit c606ea0

Browse files
committed
refactor: drop all unused _expression_parsing
Copied over the docs
1 parent 73656f1 commit c606ea0

File tree

2 files changed

+24
-122
lines changed

2 files changed

+24
-122
lines changed

narwhals/_compliant/expr.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ def from_column_indices(
323323
context: _FullContext,
324324
) -> Self: ...
325325

326-
# https://github.com/narwhals-dev/narwhals/blob/35cef0b1e2c892fb24aa730902b08b6994008c18/narwhals/_protocols.py#L135
327326
def _reuse_series_implementation(
328327
self: Self,
329328
attr: str,
@@ -332,6 +331,20 @@ def _reuse_series_implementation(
332331
call_kwargs: dict[str, Any] | None = None,
333332
**expressifiable_args: Any,
334333
) -> Self:
334+
"""Reuse Series implementation for expression.
335+
336+
If Series.foo is already defined, and we'd like Expr.foo to be the same, we can
337+
leverage this method to do that for us.
338+
339+
Arguments:
340+
attr: name of method.
341+
returns_scalar: whether the Series version returns a scalar. In this case,
342+
the expression version should return a 1-row Series.
343+
call_kwargs: non-expressifiable args which we may need to reuse in `agg` or `over`,
344+
such as `ddof` for `std` and `var`.
345+
expressifiable_args: keyword arguments to pass to function, which may
346+
be expressifiable (e.g. `nw.col('a').is_between(3, nw.col('b')))`).
347+
"""
335348
func = partial(
336349
self._reuse_series_inner,
337350
method_name=attr,
@@ -394,6 +407,16 @@ def _reuse_series_inner(
394407
def _reuse_series_namespace_implementation(
395408
self: Self, series_namespace: str, attr: str, **kwargs: Any
396409
) -> Self:
410+
"""Reuse Series implementation for expression.
411+
412+
Just like `_reuse_series_implementation`, but for e.g. `Expr.dt.foo` instead
413+
of `Expr.foo`.
414+
415+
Arguments:
416+
series_namespace: The Series namespace (e.g. `dt`, `cat`, `str`, `list`, `name`)
417+
attr: name of method.
418+
kwargs: keyword arguments to pass to function.
419+
"""
397420
return self._from_callable(
398421
lambda df: [
399422
getattr(getattr(series, series_namespace), attr)(**kwargs)

narwhals/_expression_parsing.py

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from typing import Literal
1313
from typing import Sequence
1414
from typing import TypeVar
15-
from typing import overload
1615

1716
from narwhals.dependencies import is_narwhals_series
1817
from narwhals.dependencies import is_numpy_array
@@ -30,16 +29,12 @@
3029
from narwhals._compliant import CompliantNamespace
3130
from narwhals._compliant import CompliantSeriesOrNativeExprT_co
3231
from narwhals._compliant import CompliantSeriesT_co
33-
from narwhals._pandas_like.expr import PandasLikeExpr
3432
from narwhals.expr import Expr
3533
from narwhals.typing import CompliantDataFrame
3634
from narwhals.typing import CompliantLazyFrame
37-
from narwhals.typing import CompliantSeries
3835
from narwhals.typing import IntoExpr
3936
from narwhals.typing import _1DArray
4037

41-
PandasLikeExprT = TypeVar("PandasLikeExprT", bound=PandasLikeExpr)
42-
4338
T = TypeVar("T")
4439

4540

@@ -79,122 +74,6 @@ def evaluate_into_exprs(
7974
return list(chain.from_iterable(evaluate_into_expr(df, expr) for expr in exprs))
8075

8176

82-
@overload
83-
def maybe_evaluate_expr(
84-
df: CompliantFrameT, expr: CompliantExpr[CompliantFrameT, CompliantSeriesT_co]
85-
) -> CompliantSeriesT_co: ...
86-
87-
88-
@overload
89-
def maybe_evaluate_expr(df: CompliantDataFrame[Any], expr: T) -> T: ...
90-
91-
92-
def maybe_evaluate_expr(
93-
df: Any, expr: CompliantExpr[Any, CompliantSeriesT_co] | T
94-
) -> CompliantSeriesT_co | T:
95-
"""Evaluate `expr` if it's an expression, otherwise return it as is."""
96-
if is_compliant_expr(expr):
97-
result: Sequence[CompliantSeriesT_co] = expr(df)
98-
if len(result) > 1:
99-
msg = "Multi-output expressions (e.g. `nw.all()` or `nw.col('a', 'b')`) are not supported in this context"
100-
raise ValueError(msg)
101-
return result[0]
102-
return expr
103-
104-
105-
def reuse_series_implementation(
106-
expr: PandasLikeExprT,
107-
attr: str,
108-
*,
109-
returns_scalar: bool = False,
110-
call_kwargs: dict[str, Any] | None = None,
111-
**expressifiable_args: Any,
112-
) -> PandasLikeExprT:
113-
"""Reuse Series implementation for expression.
114-
115-
If Series.foo is already defined, and we'd like Expr.foo to be the same, we can
116-
leverage this method to do that for us.
117-
118-
Arguments:
119-
expr: expression object.
120-
attr: name of method.
121-
returns_scalar: whether the Series version returns a scalar. In this case,
122-
the expression version should return a 1-row Series.
123-
call_kwargs: non-expressifiable args which we may need to reuse in `agg` or `over`,
124-
such as `ddof` for `std` and `var`.
125-
expressifiable_args: keyword arguments to pass to function, which may
126-
be expressifiable (e.g. `nw.col('a').is_between(3, nw.col('b')))`).
127-
"""
128-
plx = expr.__narwhals_namespace__()
129-
130-
def func(df: CompliantDataFrame[Any]) -> Sequence[CompliantSeries]:
131-
_kwargs = {
132-
**(call_kwargs or {}),
133-
**{
134-
arg_name: maybe_evaluate_expr(df, arg_value)
135-
for arg_name, arg_value in expressifiable_args.items()
136-
},
137-
}
138-
139-
out: list[CompliantSeries] = [
140-
plx._create_series_from_scalar( # type: ignore # noqa: PGH003
141-
getattr(series, attr)(**_kwargs),
142-
reference_series=series,
143-
)
144-
if returns_scalar
145-
else getattr(series, attr)(**_kwargs)
146-
for series in expr(df) # type: ignore # noqa: PGH003
147-
]
148-
_, aliases = evaluate_output_names_and_aliases(expr, df, [])
149-
if [s.name for s in out] != list(aliases): # pragma: no cover
150-
msg = (
151-
f"Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues\n"
152-
f"Expression aliases: {aliases}\n"
153-
f"Series names: {[s.name for s in out]}"
154-
)
155-
raise AssertionError(msg)
156-
return out
157-
158-
return plx._create_expr_from_callable( # type: ignore # noqa: PGH003
159-
func,
160-
depth=expr._depth + 1,
161-
function_name=f"{expr._function_name}->{attr}",
162-
evaluate_output_names=expr._evaluate_output_names,
163-
alias_output_names=expr._alias_output_names,
164-
call_kwargs=call_kwargs,
165-
)
166-
167-
168-
def reuse_series_namespace_implementation(
169-
expr: PandasLikeExprT,
170-
series_namespace: str,
171-
attr: str,
172-
**kwargs: Any,
173-
) -> PandasLikeExprT:
174-
"""Reuse Series implementation for expression.
175-
176-
Just like `reuse_series_implementation`, but for e.g. `Expr.dt.foo` instead
177-
of `Expr.foo`.
178-
179-
Arguments:
180-
expr: expression object.
181-
series_namespace: The Series namespace (e.g. `dt`, `cat`, `str`, `list`, `name`)
182-
attr: name of method.
183-
kwargs: keyword arguments to pass to function.
184-
"""
185-
plx = expr.__narwhals_namespace__()
186-
return plx._create_expr_from_callable( # type: ignore # noqa: PGH003
187-
lambda df: [
188-
getattr(getattr(series, series_namespace), attr)(**kwargs)
189-
for series in expr(df)
190-
],
191-
depth=expr._depth + 1,
192-
function_name=f"{expr._function_name}->{series_namespace}.{attr}",
193-
evaluate_output_names=expr._evaluate_output_names,
194-
alias_output_names=expr._alias_output_names,
195-
)
196-
197-
19877
def is_elementary_expression(expr: CompliantExpr[Any, Any]) -> bool:
19978
"""Check if expr is elementary.
20079

0 commit comments

Comments
 (0)