Skip to content

Commit b52216a

Browse files
committed
feat(api): add argmax and argmin column methods
1 parent ed91344 commit b52216a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

ibis/expr/operations/reductions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ class Min(Filterable, Reduction):
191191
output_dtype = rlz.dtype_like('arg')
192192

193193

194+
@public
195+
class ArgMax(Filterable, Reduction):
196+
arg = rlz.column(rlz.any)
197+
key = rlz.column(rlz.any)
198+
output_dtype = rlz.dtype_like("arg")
199+
200+
201+
@public
202+
class ArgMin(Filterable, Reduction):
203+
arg = rlz.column(rlz.any)
204+
key = rlz.column(rlz.any)
205+
output_dtype = rlz.dtype_like("arg")
206+
207+
194208
@public
195209
class ApproxCountDistinct(Filterable, Reduction):
196210
"""Approximate number of unique values using HyperLogLog algorithm.

ibis/expr/types/generic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,15 +652,33 @@ def approx_median(
652652
return ops.ApproxMedian(self, where).to_expr().name("approx_median")
653653

654654
def max(self, where: ir.BooleanValue | None = None) -> Scalar:
655+
"""Return the maximum of a column."""
655656
import ibis.expr.operations as ops
656657

657658
return ops.Max(self, where).to_expr().name("max")
658659

659660
def min(self, where: ir.BooleanValue | None = None) -> Scalar:
661+
"""Return the minimum of a column."""
660662
import ibis.expr.operations as ops
661663

662664
return ops.Min(self, where).to_expr().name("min")
663665

666+
def argmax(
667+
self, key: ir.Value, where: ir.BooleanValue | None = None
668+
) -> Scalar:
669+
"""Return the value of `self` that maximizes `key`."""
670+
import ibis.expr.operations as ops
671+
672+
return ops.ArgMax(self, key=key, where=where).to_expr()
673+
674+
def argmin(
675+
self, key: ir.Value, where: ir.BooleanValue | None = None
676+
) -> Scalar:
677+
"""Return the value of `self` that minimizes `key`."""
678+
import ibis.expr.operations as ops
679+
680+
return ops.ArgMin(self, key=key, where=where).to_expr()
681+
664682
def nunique(
665683
self, where: ir.BooleanValue | None = None
666684
) -> ir.IntegerScalar:

0 commit comments

Comments
 (0)