Skip to content

Commit ab6023c

Browse files
committed
Add ntile window function
1 parent d5bc309 commit ab6023c

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

python/datafusion/functions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,3 +1907,31 @@ def cume_dist() -> Expr:
19071907
ref:`_window_functions` online documentation.
19081908
"""
19091909
return Expr(f.cume_dist())
1910+
1911+
1912+
def ntile(groups: int) -> Expr:
1913+
"""Create a n-tile window function.
1914+
1915+
This window function orders the window frame into a give number of groups based on
1916+
the ordering criteria. It then returns which group the current row is assigned to.
1917+
Here is an example of a dataframe with a window ordered by descending ``points``
1918+
and the associated n-tile function.
1919+
1920+
```
1921+
+--------+-------+
1922+
| points | ntile |
1923+
+--------+-------+
1924+
| 120 | 1 |
1925+
| 100 | 1 |
1926+
| 80 | 2 |
1927+
| 60 | 2 |
1928+
| 40 | 3 |
1929+
| 20 | 3 |
1930+
+--------+-------+
1931+
```
1932+
1933+
To set window function parameters use the window builder approach described in the
1934+
ref:`_window_functions` online documentation.
1935+
"""
1936+
# Developer note: ntile only accepts literal values.
1937+
return Expr(f.ntile(Expr.literal(groups).expr))

python/datafusion/tests/test_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def test_distinct():
298298
),
299299
(
300300
"ntile",
301-
f.window("ntile", [literal(2)], order_by=[f.order_by(column("c"))]),
301+
f.ntile(2).order_by(column("c").sort()).build(),
302302
[1, 1, 2],
303303
),
304304
("lead", f.lead(column("b")).order_by(column("b").sort()).build(), [5, 6, None]),

src/functions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,11 @@ pub fn cume_dist() -> PyExpr {
894894
window_function::cume_dist().into()
895895
}
896896

897+
#[pyfunction]
898+
pub fn ntile(arg: PyExpr) -> PyExpr {
899+
window_function::ntile(arg.into()).into()
900+
}
901+
897902
pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
898903
m.add_wrapped(wrap_pyfunction!(abs))?;
899904
m.add_wrapped(wrap_pyfunction!(acos))?;
@@ -1078,13 +1083,15 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
10781083
m.add_wrapped(wrap_pyfunction!(array_slice))?;
10791084
m.add_wrapped(wrap_pyfunction!(flatten))?;
10801085

1086+
// Window Functions
10811087
m.add_wrapped(wrap_pyfunction!(lead))?;
10821088
m.add_wrapped(wrap_pyfunction!(lag))?;
10831089
m.add_wrapped(wrap_pyfunction!(row_number))?;
10841090
m.add_wrapped(wrap_pyfunction!(rank))?;
10851091
m.add_wrapped(wrap_pyfunction!(dense_rank))?;
10861092
m.add_wrapped(wrap_pyfunction!(percent_rank))?;
10871093
m.add_wrapped(wrap_pyfunction!(cume_dist))?;
1094+
m.add_wrapped(wrap_pyfunction!(ntile))?;
10881095

10891096
Ok(())
10901097
}

0 commit comments

Comments
 (0)