diff --git a/narwhals/pandas_like/dataframe.py b/narwhals/pandas_like/dataframe.py index 9ddb5131f..108ca4363 100644 --- a/narwhals/pandas_like/dataframe.py +++ b/narwhals/pandas_like/dataframe.py @@ -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 @@ -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): diff --git a/narwhals/pandas_like/expr.py b/narwhals/pandas_like/expr.py index dd90995b3..24bc3e2a3 100644 --- a/narwhals/pandas_like/expr.py +++ b/narwhals/pandas_like/expr.py @@ -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, @@ -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( @@ -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, @@ -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( @@ -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( diff --git a/narwhals/pandas_like/namespace.py b/narwhals/pandas_like/namespace.py index df5c5a901..29793d981 100644 --- a/narwhals/pandas_like/namespace.py +++ b/narwhals/pandas_like/namespace.py @@ -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): @@ -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, @@ -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], @@ -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, diff --git a/narwhals/pandas_like/series.py b/narwhals/pandas_like/series.py index 39003dd9e..b99a38f3b 100644 --- a/narwhals/pandas_like/series.py +++ b/narwhals/pandas_like/series.py @@ -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,