Skip to content

Commit

Permalink
docs(examples): add having example for GroupedTable (#10457)
Browse files Browse the repository at this point in the history
<!--
Thanks for taking the time to contribute to Ibis!

Please ensure that your pull request title matches the conventional
commits
specification: https://www.conventionalcommits.org/en/v1.0.0/
-->

## Description of changes

This adds a
[`having`](https://ibis-project.org/reference/expression-tables#ibis.expr.types.groupby.GroupedTable.having)
example for usage on a `GroupedTable`. I followed it up with an
`aggregation` to show the removal of the "a" rows.

It could be useful also to show the `having` method without aggregation
results in a `GroupedTable,` but I wasn't 100% sure if that would be
worth showing.
  • Loading branch information
IndexSeek authored Nov 9, 2024
1 parent 72e6f7b commit 130e4d5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ibis/expr/types/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ def having(self, *predicates: ir.BooleanScalar) -> GroupedTable:
-------
GroupedTable
A grouped table expression
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
... {"grouper": ["a", "a", "a", "b", "b", "c"], "values": [1, 2, 3, 1, 2, 1]}
... )
>>> expr = (
... t.group_by(t.grouper)
... .having(t.count() < 3)
... .aggregate(values_count=t.count(), values_sum=t.values.sum())
... .order_by(t.grouper)
... )
>>> expr
┏━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ grouper ┃ values_count ┃ values_sum ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ string │ int64 │ int64 │
├─────────┼──────────────┼────────────┤
│ b │ 2 │ 3 │
│ c │ 1 │ 1 │
└─────────┴──────────────┴────────────┘
"""
table = self.table.to_expr()
havings = table.bind(*predicates)
Expand Down

0 comments on commit 130e4d5

Please sign in to comment.