Skip to content

Commit 388f715

Browse files
committed
refactor(DRAFT): start migrating PandasLike*
1 parent f16ece2 commit 388f715

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

narwhals/_pandas_like/dataframe.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import numpy as np
1212

13+
from narwhals._compliant import EagerDataFrame
1314
from narwhals._expression_parsing import evaluate_into_exprs
1415
from narwhals._pandas_like.series import PANDAS_TO_NUMPY_DTYPE_MISSING
1516
from narwhals._pandas_like.series import PandasLikeSeries
@@ -26,6 +27,8 @@
2627
from narwhals._pandas_like.utils import select_columns_by_name
2728
from narwhals.dependencies import is_numpy_array_1d
2829
from narwhals.exceptions import InvalidOperationError
30+
from narwhals.typing import CompliantDataFrame
31+
from narwhals.typing import CompliantLazyFrame
2932
from narwhals.utils import Implementation
3033
from narwhals.utils import check_column_exists
3134
from narwhals.utils import generate_temporary_column_name
@@ -54,8 +57,6 @@
5457
from narwhals.typing import _2DArray
5558
from narwhals.utils import Version
5659

57-
from narwhals.typing import CompliantDataFrame
58-
from narwhals.typing import CompliantLazyFrame
5960

6061
CLASSICAL_NUMPY_DTYPES: frozenset[np.dtype[Any]] = frozenset(
6162
[
@@ -83,7 +84,7 @@
8384
)
8485

8586

86-
class PandasLikeDataFrame(CompliantDataFrame["PandasLikeSeries"], CompliantLazyFrame):
87+
class PandasLikeDataFrame(EagerDataFrame["PandasLikeSeries"], CompliantLazyFrame):
8788
# --- not in the spec ---
8889
def __init__(
8990
self: Self,

narwhals/_pandas_like/expr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Mapping
99
from typing import Sequence
1010

11-
from narwhals._compliant import CompliantExpr
11+
from narwhals._compliant import EagerExpr
1212
from narwhals._expression_parsing import ExprKind
1313
from narwhals._expression_parsing import evaluate_output_names_and_aliases
1414
from narwhals._expression_parsing import is_elementary_expression
@@ -69,7 +69,7 @@ def window_kwargs_to_pandas_equivalent(
6969
return pandas_kwargs
7070

7171

72-
class PandasLikeExpr(CompliantExpr["PandasLikeDataFrame", PandasLikeSeries]):
72+
class PandasLikeExpr(EagerExpr["PandasLikeDataFrame", PandasLikeSeries]):
7373
def __init__(
7474
self: Self,
7575
call: Callable[[PandasLikeDataFrame], Sequence[PandasLikeSeries]],

narwhals/_pandas_like/namespace.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Literal
1212
from typing import Sequence
1313

14-
from narwhals._compliant import CompliantNamespace
14+
from narwhals._compliant import EagerNamespace
1515
from narwhals._expression_parsing import combine_alias_output_names
1616
from narwhals._expression_parsing import combine_evaluate_output_names
1717
from narwhals._pandas_like.dataframe import PandasLikeDataFrame
@@ -40,7 +40,15 @@
4040
_Scalar: TypeAlias = Any
4141

4242

43-
class PandasLikeNamespace(CompliantNamespace[PandasLikeDataFrame, PandasLikeSeries]):
43+
class PandasLikeNamespace(EagerNamespace[PandasLikeDataFrame, PandasLikeSeries]):
44+
@property
45+
def _expr(self) -> type[PandasLikeExpr]:
46+
return PandasLikeExpr
47+
48+
@property
49+
def _series(self) -> type[PandasLikeSeries]:
50+
return PandasLikeSeries
51+
4452
@property
4553
def selectors(self: Self) -> PandasSelectorNamespace:
4654
return PandasSelectorNamespace(self)

narwhals/_pandas_like/series.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import cast
1010
from typing import overload
1111

12+
from narwhals._compliant import EagerSeries
1213
from narwhals._pandas_like.series_cat import PandasLikeSeriesCatNamespace
1314
from narwhals._pandas_like.series_dt import PandasLikeSeriesDateTimeNamespace
1415
from narwhals._pandas_like.series_list import PandasLikeSeriesListNamespace
@@ -24,7 +25,6 @@
2425
from narwhals._pandas_like.utils import set_index
2526
from narwhals.dependencies import is_numpy_scalar
2627
from narwhals.exceptions import InvalidOperationError
27-
from narwhals.typing import CompliantSeries
2828
from narwhals.utils import Implementation
2929
from narwhals.utils import import_dtypes_module
3030
from narwhals.utils import validate_backend_version
@@ -90,7 +90,7 @@
9090
}
9191

9292

93-
class PandasLikeSeries(CompliantSeries):
93+
class PandasLikeSeries(EagerSeries[Any]):
9494
def __init__(
9595
self: Self,
9696
native_series: Any,
@@ -112,6 +112,10 @@ def __init__(
112112
# the length of the whole dataframe, we just extract the scalar.
113113
self._broadcast = False
114114

115+
@property
116+
def native(self) -> Any:
117+
return self._native_series
118+
115119
def __native_namespace__(self: Self) -> ModuleType:
116120
if self._implementation in {
117121
Implementation.PANDAS,
@@ -139,7 +143,7 @@ def __getitem__(self: Self, idx: int | slice | Sequence[int]) -> Any | Self:
139143

140144
def _change_version(self: Self, version: Version) -> Self:
141145
return self.__class__(
142-
self._native_series,
146+
self.native,
143147
implementation=self._implementation,
144148
backend_version=self._backend_version,
145149
version=version,

0 commit comments

Comments
 (0)