diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index a784da6a8..3e2ab225c 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -118,6 +118,9 @@ def select( self._dataframe.select(*exprs, **named_exprs), ) + def rename(self, mapping: dict[str, str]) -> Self: + return self._from_dataframe(self._dataframe.rename(mapping)) + def filter(self, *predicates: IntoExpr | Iterable[IntoExpr]) -> Self: predicates, _ = self._flatten_and_extract(*predicates) return self._from_dataframe( diff --git a/narwhals/pandas_like/dataframe.py b/narwhals/pandas_like/dataframe.py index bc24d2a76..e7280bd92 100644 --- a/narwhals/pandas_like/dataframe.py +++ b/narwhals/pandas_like/dataframe.py @@ -121,7 +121,7 @@ def with_columns( ) -> Self: new_series = evaluate_into_exprs(self, *exprs, **named_exprs) df = self._dataframe.assign( - **{series.name: series._series for series in new_series} + **{series.name: validate_dataframe_comparand(series) for series in new_series} ) return self._from_dataframe(df) diff --git a/narwhals/pandas_like/series.py b/narwhals/pandas_like/series.py index c9403ba90..6ef9bc63e 100644 --- a/narwhals/pandas_like/series.py +++ b/narwhals/pandas_like/series.py @@ -41,6 +41,9 @@ def _from_series(self, series: Any) -> Self: implementation=self._implementation, ) + def __len__(self) -> int: + return self.shape[0] + @property def name(self) -> str: return self._name diff --git a/narwhals/pandas_like/utils.py b/narwhals/pandas_like/utils.py index d3be72af7..038dc8162 100644 --- a/narwhals/pandas_like/utils.py +++ b/narwhals/pandas_like/utils.py @@ -70,7 +70,7 @@ def validate_dataframe_comparand(other: Any) -> Any: if isinstance(other, PandasSeries): if other.len() == 1: # broadcast - return item(other) + return item(other._series) return other._series return other @@ -106,7 +106,6 @@ def parse_into_expr(implementation: str, into_expr: IntoExpr) -> PandasExpr: return into_expr._call(plx) # type: ignore[no-any-return] if isinstance(into_expr, str): return plx.col(into_expr) - # todo: what if it's a Series? msg = f"Expected IntoExpr, got {type(into_expr)}" raise TypeError(msg)