Skip to content

Commit ecb3767

Browse files
committed
feat: Fill out functions reprs
1 parent b7bbbef commit ecb3767

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

narwhals/_plan/functions.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Abs(Function):
3131
def function_options(self) -> FunctionOptions:
3232
return FunctionOptions.elementwise()
3333

34+
def __repr__(self) -> str:
35+
return "abs"
36+
3437

3538
class Hist(Function):
3639
"""Only supported for `Series` so far."""
@@ -43,6 +46,9 @@ class Hist(Function):
4346
def function_options(self) -> FunctionOptions:
4447
return FunctionOptions.groupwise()
4548

49+
def __repr__(self) -> str:
50+
return "hist"
51+
4652

4753
class HistBins(Hist):
4854
"""Subclasses for each variant."""
@@ -76,12 +82,18 @@ class NullCount(Function):
7682
def function_options(self) -> FunctionOptions:
7783
return FunctionOptions.aggregation()
7884

85+
def __repr__(self) -> str:
86+
return "null_count"
87+
7988

8089
class Pow(Function):
8190
@property
8291
def function_options(self) -> FunctionOptions:
8392
return FunctionOptions.elementwise()
8493

94+
def __repr__(self) -> str:
95+
return "pow"
96+
8597

8698
class FillNull(Function):
8799
__slots__ = ("value",)
@@ -92,6 +104,9 @@ class FillNull(Function):
92104
def function_options(self) -> FunctionOptions:
93105
return FunctionOptions.elementwise()
94106

107+
def __repr__(self) -> str:
108+
return "fill_null"
109+
95110

96111
class FillNullWithStrategy(Function):
97112
"""We don't support this variant in a lot of backends, so worth keeping it split out.
@@ -114,6 +129,9 @@ def function_options(self) -> FunctionOptions:
114129
else FunctionOptions.groupwise()
115130
)
116131

132+
def __repr__(self) -> str:
133+
return "fill_null_with_strategy"
134+
117135

118136
class Shift(Function):
119137
__slots__ = ("n",)
@@ -125,24 +143,36 @@ class Shift(Function):
125143
def function_options(self) -> FunctionOptions:
126144
return FunctionOptions.length_preserving()
127145

146+
def __repr__(self) -> str:
147+
return "shift"
148+
128149

129150
class DropNulls(Function):
130151
@property
131152
def function_options(self) -> FunctionOptions:
132153
return FunctionOptions.row_separable()
133154

155+
def __repr__(self) -> str:
156+
return "drop_nulls"
157+
134158

135159
class Mode(Function):
136160
@property
137161
def function_options(self) -> FunctionOptions:
138162
return FunctionOptions.groupwise()
139163

164+
def __repr__(self) -> str:
165+
return "mode"
166+
140167

141168
class Skew(Function):
142169
@property
143170
def function_options(self) -> FunctionOptions:
144171
return FunctionOptions.aggregation()
145172

173+
def __repr__(self) -> str:
174+
return "skew"
175+
146176

147177
class Rank(Function):
148178
__slots__ = ("options",)
@@ -153,12 +183,18 @@ class Rank(Function):
153183
def function_options(self) -> FunctionOptions:
154184
return FunctionOptions.groupwise()
155185

186+
def __repr__(self) -> str:
187+
return "rank"
188+
156189

157190
class Clip(Function):
158191
@property
159192
def function_options(self) -> FunctionOptions:
160193
return FunctionOptions.elementwise()
161194

195+
def __repr__(self) -> str:
196+
return "clip"
197+
162198

163199
class CumAgg(Function):
164200
__slots__ = ("reverse",)
@@ -170,6 +206,18 @@ class CumAgg(Function):
170206
def function_options(self) -> FunctionOptions:
171207
return FunctionOptions.length_preserving()
172208

209+
def __repr__(self) -> str:
210+
tp = type(self)
211+
if tp is CumAgg:
212+
return tp.__name__
213+
m: dict[type[CumAgg], str] = {
214+
CumCount: "count",
215+
CumMin: "min",
216+
CumMax: "max",
217+
CumProd: "prod",
218+
}
219+
return f"cum_{m[tp]}"
220+
173221

174222
class RollingWindow(Function):
175223
__slots__ = ("options",)
@@ -181,6 +229,18 @@ def function_options(self) -> FunctionOptions:
181229
"""https://github.com/pola-rs/polars/blob/dafd0a2d0e32b52bcfa4273bffdd6071a0d5977a/crates/polars-plan/src/dsl/function_expr/mod.rs#L1276."""
182230
return FunctionOptions.length_preserving()
183231

232+
def __repr__(self) -> str:
233+
tp = type(self)
234+
if tp is RollingWindow:
235+
return tp.__name__
236+
m: dict[type[RollingWindow], str] = {
237+
RollingSum: "sum",
238+
RollingMean: "mean",
239+
RollingVar: "var",
240+
RollingStd: "std",
241+
}
242+
return f"rolling_{m[tp]}"
243+
184244

185245
class CumCount(CumAgg): ...
186246

@@ -211,12 +271,18 @@ class Diff(Function):
211271
def function_options(self) -> FunctionOptions:
212272
return FunctionOptions.length_preserving()
213273

274+
def __repr__(self) -> str:
275+
return "diff"
276+
214277

215278
class Unique(Function):
216279
@property
217280
def function_options(self) -> FunctionOptions:
218281
return FunctionOptions.groupwise()
219282

283+
def __repr__(self) -> str:
284+
return "unique"
285+
220286

221287
class Round(Function):
222288
__slots__ = ("decimals",)
@@ -227,6 +293,9 @@ class Round(Function):
227293
def function_options(self) -> FunctionOptions:
228294
return FunctionOptions.elementwise()
229295

296+
def __repr__(self) -> str:
297+
return "round"
298+
230299

231300
class SumHorizontal(Function):
232301
@property
@@ -281,6 +350,9 @@ class EwmMean(Function):
281350
def function_options(self) -> FunctionOptions:
282351
return FunctionOptions.length_preserving()
283352

353+
def __repr__(self) -> str:
354+
return "ewm_mean"
355+
284356

285357
class ReplaceStrict(Function):
286358
__slots__ = ("new", "old", "return_dtype")
@@ -293,6 +365,9 @@ class ReplaceStrict(Function):
293365
def function_options(self) -> FunctionOptions:
294366
return FunctionOptions.elementwise()
295367

368+
def __repr__(self) -> str:
369+
return "replace_strict"
370+
296371

297372
class GatherEvery(Function):
298373
__slots__ = ("n", "offset")
@@ -304,6 +379,9 @@ class GatherEvery(Function):
304379
def function_options(self) -> FunctionOptions:
305380
return FunctionOptions.groupwise()
306381

382+
def __repr__(self) -> str:
383+
return "gather_every"
384+
307385

308386
class MapBatches(Function):
309387
__slots__ = ("function", "is_elementwise", "return_dtype", "returns_scalar")
@@ -322,3 +400,6 @@ def function_options(self) -> FunctionOptions:
322400
if self.returns_scalar:
323401
options = options.with_flags(FunctionFlags.RETURNS_SCALAR)
324402
return options
403+
404+
def __repr__(self) -> str:
405+
return "map_batches"

0 commit comments

Comments
 (0)