Skip to content

Commit

Permalink
fixup broadcasting!
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 15, 2024
1 parent 7929bdf commit c1e87b5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion narwhals/pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions narwhals/pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions narwhals/pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c1e87b5

Please sign in to comment.