Skip to content

Commit 169b829

Browse files
authored
perf: improve with_columns for pandas-like when using nw.lit (#1324)
* perf: improve with_columns for pandas-like when using nw.lit * reword comment
1 parent ab7a2d9 commit 169b829

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

narwhals/_pandas_like/dataframe.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,13 @@ def with_columns(
395395
if not new_columns and len(self) == 0:
396396
return self
397397

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

409407
if fast_path:

narwhals/_pandas_like/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def validate_dataframe_comparand(index: Any, other: Any) -> Any:
129129
if other.len() == 1:
130130
# broadcast
131131
s = other._native_series
132-
return s.__class__(s.iloc[0], index=index, dtype=s.dtype)
132+
return s.__class__(s.iloc[0], index=index, dtype=s.dtype, name=s.name)
133133
if other._native_series.index is not index:
134134
return set_axis(
135135
other._native_series,

0 commit comments

Comments
 (0)