Skip to content

Commit

Permalink
move maybe_evaluate_expr out of for loop (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi authored Oct 18, 2024
1 parent 8e5cc66 commit 876303d
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions narwhals/_expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,21 @@ def reuse_series_implementation(
plx = expr.__narwhals_namespace__()

def func(df: CompliantDataFrame) -> list[CompliantSeries]:
out: list[CompliantSeries] = []
for column in expr._call(df): # type: ignore[arg-type]
_out = getattr(column, attr)(
*[maybe_evaluate_expr(df, arg) for arg in args],
**{
arg_name: maybe_evaluate_expr(df, arg_value)
for arg_name, arg_value in kwargs.items()
},
_args = [maybe_evaluate_expr(df, arg) for arg in args]
_kwargs = {
arg_name: maybe_evaluate_expr(df, arg_value)
for arg_name, arg_value in kwargs.items()
}

out: list[CompliantSeries] = [
plx._create_series_from_scalar(
getattr(column, attr)(*_args, **_kwargs),
column, # type: ignore[arg-type]
)
if returns_scalar:
out.append(plx._create_series_from_scalar(_out, column)) # type: ignore[arg-type]
else:
out.append(_out)
if returns_scalar
else getattr(column, attr)(*_args, **_kwargs)
for column in expr._call(df) # type: ignore[arg-type]
]
if expr._output_names is not None and (
[s.name for s in out] != expr._output_names
): # pragma: no cover
Expand Down

0 comments on commit 876303d

Please sign in to comment.