Skip to content

Commit c1e87b5

Browse files
committed
fixup broadcasting!
1 parent 7929bdf commit c1e87b5

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

narwhals/dataframe.py

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def select(
118118
self._dataframe.select(*exprs, **named_exprs),
119119
)
120120

121+
def rename(self, mapping: dict[str, str]) -> Self:
122+
return self._from_dataframe(self._dataframe.rename(mapping))
123+
121124
def filter(self, *predicates: IntoExpr | Iterable[IntoExpr]) -> Self:
122125
predicates, _ = self._flatten_and_extract(*predicates)
123126
return self._from_dataframe(

narwhals/pandas_like/dataframe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def with_columns(
121121
) -> Self:
122122
new_series = evaluate_into_exprs(self, *exprs, **named_exprs)
123123
df = self._dataframe.assign(
124-
**{series.name: series._series for series in new_series}
124+
**{series.name: validate_dataframe_comparand(series) for series in new_series}
125125
)
126126
return self._from_dataframe(df)
127127

narwhals/pandas_like/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def _from_series(self, series: Any) -> Self:
4141
implementation=self._implementation,
4242
)
4343

44+
def __len__(self) -> int:
45+
return self.shape[0]
46+
4447
@property
4548
def name(self) -> str:
4649
return self._name

narwhals/pandas_like/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def validate_dataframe_comparand(other: Any) -> Any:
7070
if isinstance(other, PandasSeries):
7171
if other.len() == 1:
7272
# broadcast
73-
return item(other)
73+
return item(other._series)
7474
return other._series
7575
return other
7676

@@ -106,7 +106,6 @@ def parse_into_expr(implementation: str, into_expr: IntoExpr) -> PandasExpr:
106106
return into_expr._call(plx) # type: ignore[no-any-return]
107107
if isinstance(into_expr, str):
108108
return plx.col(into_expr)
109-
# todo: what if it's a Series?
110109
msg = f"Expected IntoExpr, got {type(into_expr)}"
111110
raise TypeError(msg)
112111

0 commit comments

Comments
 (0)