Skip to content

Commit

Permalink
docs(ranking): add ranking function docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Oct 12, 2023
1 parent bf08a2a commit 750bfeb
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/_quarto.yml
Expand Up @@ -230,6 +230,21 @@ quartodoc:
- name: row_number
dynamic: true
signature_name: full
- name: rank
dynamic: true
signature_name: full
- name: dense_rank
dynamic: true
signature_name: full
- name: percent_rank
dynamic: true
signature_name: full
- name: cume_dist
dynamic: true
signature_name: full
- name: ntile
dynamic: true
signature_name: full
- name: window
dynamic: true
signature_name: full
Expand Down
92 changes: 90 additions & 2 deletions ibis/expr/api.py
Expand Up @@ -1093,12 +1093,62 @@ def dense_rank() -> ir.IntegerColumn:


def percent_rank() -> ir.FloatingColumn:
"""Return the relative rank of the values in the column."""
"""Return the relative rank of the values in the column.
Returns
-------
FloatingColumn
The percent rank
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
>>> t.mutate(pct_rank=ibis.percent_rank().over(order_by=t.values))
┏━━━━━━━━┳━━━━━━━━━━┓
┃ values ┃ pct_rank ┃
┡━━━━━━━━╇━━━━━━━━━━┩
│ int64 │ float64 │
├────────┼──────────┤
│ 1 │ 0.0 │
│ 1 │ 0.0 │
│ 2 │ 0.4 │
│ 2 │ 0.4 │
│ 2 │ 0.4 │
│ 3 │ 1.0 │
└────────┴──────────┘
"""
return ops.PercentRank().to_expr()


def cume_dist() -> ir.FloatingColumn:
"""Return the cumulative distribution over a window."""
"""Return the cumulative distribution over a window.
Returns
-------
FloatingColumn
The cumulative distribution
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
>>> t.mutate(dist=ibis.cume_dist().over(order_by=t.values))
┏━━━━━━━━┳━━━━━━━━━━┓
┃ values ┃ dist ┃
┡━━━━━━━━╇━━━━━━━━━━┩
│ int64 │ float64 │
├────────┼──────────┤
│ 1 │ 0.333333 │
│ 1 │ 0.333333 │
│ 2 │ 0.833333 │
│ 2 │ 0.833333 │
│ 2 │ 0.833333 │
│ 3 │ 1.000000 │
└────────┴──────────┘
"""
return ops.CumeDist().to_expr()


Expand All @@ -1109,6 +1159,25 @@ def ntile(buckets: int | ir.IntegerValue) -> ir.IntegerColumn:
----------
buckets
Number of buckets to partition into
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
>>> t.mutate(ntile=ibis.ntile(2).over(order_by=t.values))
┏━━━━━━━━┳━━━━━━━┓
┃ values ┃ ntile ┃
┡━━━━━━━━╇━━━━━━━┩
│ int64 │ int64 │
├────────┼───────┤
│ 1 │ 0 │
│ 1 │ 0 │
│ 2 │ 0 │
│ 2 │ 1 │
│ 2 │ 1 │
│ 3 │ 1 │
└────────┴───────┘
"""
return ops.NTile(buckets).to_expr()

Expand All @@ -1120,6 +1189,25 @@ def row_number() -> ir.IntegerColumn:
-------
IntegerColumn
A column expression enumerating rows
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2, 1, 2, 3, 2]})
>>> t.mutate(rownum=ibis.row_number())
┏━━━━━━━━┳━━━━━━━━┓
┃ values ┃ rownum ┃
┡━━━━━━━━╇━━━━━━━━┩
│ int64 │ int64 │
├────────┼────────┤
│ 1 │ 0 │
│ 2 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
│ 3 │ 4 │
│ 2 │ 5 │
└────────┴────────┘
"""
return ops.RowNumber().to_expr()

Expand Down

0 comments on commit 750bfeb

Please sign in to comment.