Skip to content

Commit fb97947

Browse files
committed
we can make typing...better?
1 parent 8cd648b commit fb97947

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

narwhals/pandas_like/dataframe.py

+23-22
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def __init__(
4343
def columns(self) -> list[str]:
4444
return self.dataframe.columns.tolist()
4545

46+
def _dispatch_to_lazy(self, method: str, *args: Any, **kwargs: Any) -> Self:
47+
return getattr(self.lazy(), method)(*args, **kwargs).collect()
48+
4649
def __repr__(self) -> str: # pragma: no cover
4750
header = f" Standard DataFrame (api_version={self.api_version}) "
4851
length = len(header)
@@ -100,42 +103,40 @@ def select(
100103
self,
101104
*exprs: IntoExpr | Iterable[IntoExpr],
102105
**named_exprs: IntoExpr,
103-
) -> DataFrameT:
104-
return self.lazy().select(*exprs, **named_exprs).collect()
106+
) -> Self:
107+
return self._dispatch_to_lazy("select", *exprs, **named_exprs)
105108

106109
def filter(
107110
self,
108111
*predicates: IntoExpr | Iterable[IntoExpr],
109-
) -> DataFrameT:
110-
return self.lazy().filter(*predicates).collect()
112+
) -> Self:
113+
return self._dispatch_to_lazy("filter", *predicates)
111114

112115
def with_columns(
113116
self,
114117
*exprs: IntoExpr | Iterable[IntoExpr],
115118
**named_exprs: IntoExpr,
116-
) -> DataFrameT:
117-
return self.lazy().with_columns(*exprs, **named_exprs).collect()
119+
) -> Self:
120+
return self._dispatch_to_lazy("with_columns", *exprs, **named_exprs)
118121

119122
def sort(
120123
self,
121124
by: str | Iterable[str],
122125
*more_by: str,
123126
descending: bool | Iterable[bool] = False,
124-
) -> DataFrameT:
125-
return self.lazy().sort(by, *more_by, descending=descending).collect()
127+
) -> Self:
128+
return self._dispatch_to_lazy("sort", by, *more_by, descending=descending)
126129

127130
def join(
128131
self,
129-
other: DataFrameT,
132+
other: Self,
130133
*,
131134
how: Literal["left", "inner", "outer"] = "inner",
132135
left_on: str | list[str],
133136
right_on: str | list[str],
134-
) -> DataFrameT:
135-
return (
136-
self.lazy()
137-
.join(other.lazy(), how=how, left_on=left_on, right_on=right_on)
138-
.collect()
137+
) -> Self:
138+
return self._dispatch_to_lazy(
139+
"join", other.lazy(), how=how, left_on=left_on, right_on=right_on
139140
)
140141

141142
def lazy(self) -> LazyFrame:
@@ -145,14 +146,14 @@ def lazy(self) -> LazyFrame:
145146
implementation=self._implementation,
146147
)
147148

148-
def head(self, n: int) -> DataFrameT:
149-
return self.lazy().head(n).collect()
149+
def head(self, n: int) -> Self:
150+
return self._dispatch_to_lazy("head", n)
150151

151-
def unique(self, subset: list[str]) -> DataFrameT:
152-
return self.lazy().unique(subset).collect()
152+
def unique(self, subset: list[str]) -> Self:
153+
return self._dispatch_to_lazy("unique", subset)
153154

154-
def rename(self, mapping: dict[str, str]) -> DataFrameT:
155-
return self.lazy().rename(mapping).collect()
155+
def rename(self, mapping: dict[str, str]) -> Self:
156+
return self._dispatch_to_lazy("rename", mapping)
156157

157158
def to_numpy(self) -> Any:
158159
return self.dataframe.to_numpy()
@@ -301,7 +302,7 @@ def sort(
301302
# Other
302303
def join(
303304
self,
304-
other: LazyFrameT,
305+
other: Self,
305306
*,
306307
how: Literal["left", "inner", "outer"] = "inner",
307308
left_on: str | list[str],
@@ -332,7 +333,7 @@ def join(
332333
)
333334

334335
# Conversion
335-
def collect(self) -> DataFrameT:
336+
def collect(self) -> DataFrame:
336337
return DataFrame(
337338
self.dataframe,
338339
api_version=self.api_version,

narwhals/spec/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def lazy(self) -> LazyFrame:
189189

190190
def join(
191191
self,
192-
other: DataFrame,
192+
other: Self,
193193
*,
194194
how: Literal["inner"] = "inner",
195195
left_on: str | list[str],
@@ -255,7 +255,7 @@ def group_by(self, *keys: str | Iterable[str]) -> LazyGroupBy:
255255

256256
def join(
257257
self,
258-
other: LazyFrame,
258+
other: Self,
259259
*,
260260
how: Literal["left", "inner", "outer"] = "inner",
261261
left_on: str | list[str],

0 commit comments

Comments
 (0)