Skip to content

Commit

Permalink
perf: improve with_columns for pandas-like when using nw.lit (#1324)
Browse files Browse the repository at this point in the history
* perf: improve with_columns for pandas-like when using nw.lit

* reword comment
  • Loading branch information
MarcoGorelli authored Nov 5, 2024
1 parent ab7a2d9 commit 169b829
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,13 @@ def with_columns(
if not new_columns and len(self) == 0:
return self

# If the inputs are all Expressions which return full columns
# (as opposed to scalars), we can use a fast path (concat, instead of assign).
# If the inputs are all Expressions
# (as opposed to Series), we can use a fast path (concat, instead of assign).
# We can't use the fastpath if any input is not an expression (e.g.
# if it's a Series) because then we might be changing its flags.
# See `test_memmap` for an example of where this is necessary.
fast_path = (
all(len(s) > 1 for s in new_columns)
and all(isinstance(x, PandasLikeExpr) for x in exprs)
and all(isinstance(x, PandasLikeExpr) for (_, x) in named_exprs.items())
fast_path = all(isinstance(x, PandasLikeExpr) for x in exprs) and all(
isinstance(x, PandasLikeExpr) for (_, x) in named_exprs.items()
)

if fast_path:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def validate_dataframe_comparand(index: Any, other: Any) -> Any:
if other.len() == 1:
# broadcast
s = other._native_series
return s.__class__(s.iloc[0], index=index, dtype=s.dtype)
return s.__class__(s.iloc[0], index=index, dtype=s.dtype, name=s.name)
if other._native_series.index is not index:
return set_axis(
other._native_series,
Expand Down

0 comments on commit 169b829

Please sign in to comment.