Skip to content

Commit 291bb69

Browse files
authored
docs(examples): count, percent_rank, cume_dist, ntile example usage (#10435)
1 parent 13ebb91 commit 291bb69

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

ibis/expr/types/generic.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,25 @@ def count(self, where: ir.BooleanValue | None = None) -> ir.IntegerScalar:
21232123
-------
21242124
IntegerScalar
21252125
Number of elements in an expression
2126+
2127+
Examples
2128+
--------
2129+
>>> import ibis
2130+
>>> ibis.options.interactive = True
2131+
>>> t = ibis.memtable(
2132+
... {
2133+
... "id": [1, 2, 3, 4, 5, 6],
2134+
... "color": ["Red", "Green", "Blue", "Blue", "Red", "Blue"],
2135+
... }
2136+
... )
2137+
>>> t.count()
2138+
┌───┐
2139+
│ 6 │
2140+
└───┘
2141+
>>> t.count(where=t.color == "Blue")
2142+
┌───┐
2143+
│ 3 │
2144+
└───┘
21262145
"""
21272146
return ops.Count(self, where=self._bind_to_parent_table(where)).to_expr()
21282147

@@ -2352,11 +2371,51 @@ def dense_rank(self) -> ir.IntegerColumn:
23522371
return ibis.dense_rank().over(order_by=self)
23532372

23542373
def percent_rank(self) -> Column:
2355-
"""Return the relative rank of the values in the column."""
2374+
"""Return the relative rank of the values in the column.
2375+
2376+
Examples
2377+
--------
2378+
>>> import ibis
2379+
>>> ibis.options.interactive = True
2380+
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
2381+
>>> t.mutate(percent_rank=t.values.percent_rank())
2382+
┏━━━━━━━━┳━━━━━━━━━━━━━━┓
2383+
┃ values ┃ percent_rank ┃
2384+
┡━━━━━━━━╇━━━━━━━━━━━━━━┩
2385+
│ int64 │ float64 │
2386+
├────────┼──────────────┤
2387+
│ 1 │ 0.0 │
2388+
│ 1 │ 0.0 │
2389+
│ 2 │ 0.4 │
2390+
│ 2 │ 0.4 │
2391+
│ 2 │ 0.4 │
2392+
│ 3 │ 1.0 │
2393+
└────────┴──────────────┘
2394+
"""
23562395
return ibis.percent_rank().over(order_by=self)
23572396

23582397
def cume_dist(self) -> Column:
2359-
"""Return the cumulative distribution over a window."""
2398+
"""Return the cumulative distribution over a window.
2399+
2400+
Examples
2401+
--------
2402+
>>> import ibis
2403+
>>> ibis.options.interactive = True
2404+
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
2405+
>>> t.mutate(cume_dist=t.values.cume_dist())
2406+
┏━━━━━━━━┳━━━━━━━━━━━┓
2407+
┃ values ┃ cume_dist ┃
2408+
┡━━━━━━━━╇━━━━━━━━━━━┩
2409+
│ int64 │ float64 │
2410+
├────────┼───────────┤
2411+
│ 1 │ 0.333333 │
2412+
│ 1 │ 0.333333 │
2413+
│ 2 │ 0.833333 │
2414+
│ 2 │ 0.833333 │
2415+
│ 2 │ 0.833333 │
2416+
│ 3 │ 1.000000 │
2417+
└────────┴───────────┘
2418+
"""
23602419
return ibis.cume_dist().over(order_by=self)
23612420

23622421
def ntile(self, buckets: int | ir.IntegerValue) -> ir.IntegerColumn:
@@ -2366,6 +2425,25 @@ def ntile(self, buckets: int | ir.IntegerValue) -> ir.IntegerColumn:
23662425
----------
23672426
buckets
23682427
Number of buckets to partition into
2428+
2429+
Examples
2430+
--------
2431+
>>> import ibis
2432+
>>> ibis.options.interactive = True
2433+
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
2434+
>>> t.mutate(ntile=t.values.ntile(3))
2435+
┏━━━━━━━━┳━━━━━━━┓
2436+
┃ values ┃ ntile ┃
2437+
┡━━━━━━━━╇━━━━━━━┩
2438+
│ int64 │ int64 │
2439+
├────────┼───────┤
2440+
│ 1 │ 0 │
2441+
│ 1 │ 0 │
2442+
│ 2 │ 1 │
2443+
│ 2 │ 1 │
2444+
│ 2 │ 2 │
2445+
│ 3 │ 2 │
2446+
└────────┴───────┘
23692447
"""
23702448
return ibis.ntile(buckets).over(order_by=self)
23712449

0 commit comments

Comments
 (0)