diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 796386fad..5eab8b08f 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -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: diff --git a/narwhals/_pandas_like/utils.py b/narwhals/_pandas_like/utils.py index 8ab2cfad7..87ed2c944 100644 --- a/narwhals/_pandas_like/utils.py +++ b/narwhals/_pandas_like/utils.py @@ -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,