Skip to content

Commit

Permalink
feat(pandas): implement argmin and argmax
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 20, 2022
1 parent abf03f1 commit bf9b948
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ibis/backends/pandas/execution/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,23 @@ def execute_arbitrary_series_groupby(op, data, _, aggcontext=None, **kwargs):
return aggcontext.agg(data, how)


@execute_node.register(
(ops.ArgMin, ops.ArgMax),
SeriesGroupBy,
SeriesGroupBy,
type(None),
)
def execute_reduction_series_groupby_argidx(
op, data, key, _, aggcontext=None, **kwargs
):
method = operator.methodcaller(op.__class__.__name__.lower())

def reduce(data, key=key.obj, method=method):
return data.iloc[method(key.loc[data.index])]

return aggcontext.agg(data, reduce)


def _filtered_reduction(mask, method, data):
return method(data[mask[data.index]])

Expand Down Expand Up @@ -758,6 +775,20 @@ def execute_bit_xor_series(_, data, mask, aggcontext=None, **kwargs):
)


@execute_node.register(
(ops.ArgMin, ops.ArgMax),
pd.Series,
pd.Series,
(pd.Series, type(None)),
)
def execute_argmin_series_mask(op, data, key, mask, aggcontext=None, **kwargs):
method_name = op.__class__.__name__.lower()
masked_key = key[mask] if mask is not None else key
idx = aggcontext.agg(masked_key, method_name)
masked = data[mask] if mask is not None else data
return masked.iloc[idx]


@execute_node.register((ops.Not, ops.Negate), (bool, np.bool_))
def execute_not_bool(_, data, **kwargs):
return not data
Expand Down

0 comments on commit bf9b948

Please sign in to comment.