Skip to content

Commit

Permalink
wip unbroken
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 21, 2024
1 parent 6db674a commit 998993a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion narwhals/pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from narwhals.pandas_like.utils import horizontal_concat
from narwhals.pandas_like.utils import validate_dataframe_comparand
from narwhals.spec import DataFrame as DataFrameT
from narwhals.spec import IntoExpr
from narwhals.spec import LazyFrame as LazyFrameProtocol
from narwhals.spec import Namespace as NamespaceProtocol

Expand All @@ -23,6 +22,7 @@
from narwhals.pandas_like.group_by_object import GroupBy
from narwhals.pandas_like.group_by_object import LazyGroupBy
from narwhals.pandas_like.namespace import Namespace
from narwhals.pandas_like.utils import IntoExpr


class DataFrame(DataFrameT):
Expand Down
18 changes: 12 additions & 6 deletions narwhals/pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
from typing import Any
from typing import Callable

from narwhals.pandas_like.series import Series
from narwhals.pandas_like.utils import register_expression_call
from narwhals.spec import DataFrame as DataFrameT
from narwhals.spec import Expr as ExprT
from narwhals.spec import ExprStringNamespace as ExprStringNamespaceT
from narwhals.spec import LazyFrame as LazyFrameProtocol
from narwhals.spec import Namespace as NamespaceProtocol
from narwhals.spec import Series as SeriesProtocol

if TYPE_CHECKING:
from typing_extensions import Self

from narwhals.pandas_like.dataframe import DataFrame
from narwhals.pandas_like.dataframe import LazyFrame
from narwhals.pandas_like.series import Series


class Expr(ExprT):
def __init__( # noqa: PLR0913
self,
call: Callable[[DataFrameT | LazyFrameProtocol], list[SeriesProtocol]],
call: Callable[[DataFrame | LazyFrame], list[Series]],
*,
depth: int | None,
function_name: str | None,
Expand Down Expand Up @@ -50,6 +50,8 @@ def __repr__(self) -> str:
def from_column_names(
cls: type[Self], *column_names: str, implementation: str
) -> Self:
from narwhals.pandas_like.series import Series

return cls(
lambda df: [
Series(
Expand Down Expand Up @@ -197,7 +199,7 @@ def alias(self, name: str) -> Self:
if self._depth is None:
msg = "Unreachable code, please report a bug"
raise AssertionError(msg)
return Expr(
return self.__class__(
lambda df: [series.alias(name) for series in self.call(df)],
depth=self._depth,
function_name=self._function_name,
Expand All @@ -217,6 +219,8 @@ def __init__(self, expr: ExprT) -> None:

def ends_with(self, suffix: str) -> Expr:
# TODO make a register_expression_call for namespaces
from narwhals.pandas_like.series import Series

return Expr(
lambda df: [
Series(
Expand All @@ -234,6 +238,8 @@ def ends_with(self, suffix: str) -> Expr:
)

def strip_chars(self, characters: str = " ") -> Expr:
from narwhals.pandas_like.series import Series

return Expr(
lambda df: [
Series(
Expand Down
11 changes: 3 additions & 8 deletions narwhals/pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
from narwhals.pandas_like.utils import parse_into_exprs
from narwhals.pandas_like.utils import series_from_iterable
from narwhals.spec import AnyDataFrame
from narwhals.spec import DataFrame as DataFrameT
from narwhals.spec import IntoExpr
from narwhals.spec import LazyFrame as LazyFrameProtocol
from narwhals.spec import Namespace as NamespaceProtocol
from narwhals.spec import Series as SeriesProtocol


class Namespace(NamespaceProtocol):
Expand Down Expand Up @@ -109,7 +106,7 @@ def len(self) -> Expr:

def _create_expr_from_callable( # noqa: PLR0913
self,
func: Callable[[DataFrameT | LazyFrameProtocol], list[SeriesProtocol]],
func: Callable[[DataFrame | LazyFrame], list[Series]],
*,
depth: int,
function_name: str | None,
Expand All @@ -125,9 +122,7 @@ def _create_expr_from_callable( # noqa: PLR0913
implementation=self._implementation,
)

def _create_series_from_scalar(
self, value: Any, series: SeriesProtocol
) -> SeriesProtocol:
def _create_series_from_scalar(self, value: Any, series: Series) -> Series:
return Series(
series_from_iterable(
[value],
Expand All @@ -139,7 +134,7 @@ def _create_series_from_scalar(
implementation=self._implementation,
)

def _create_expr_from_series(self, series: SeriesProtocol) -> Expr:
def _create_expr_from_series(self, series: Series) -> Expr:
return Expr(
lambda _df: [series],
depth=0,
Expand Down
4 changes: 2 additions & 2 deletions narwhals/pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def __repr__(self) -> str: # pragma: no cover
+ "┘\n"
)

def _from_series(self, series: Any) -> Series:
return Series(
def _from_series(self, series: Any) -> Self:
return self.__class__(
series.rename(series.name, copy=False),
api_version=self.api_version,
implementation=self._implementation,
Expand Down

0 comments on commit 998993a

Please sign in to comment.