Skip to content

Commit

Permalink
feat(api): add ibis.asc expression
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainsultan committed Mar 11, 2022
1 parent 9901c15 commit f907736
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'cumulative_window',
'date',
'desc',
'asc',
'Expr',
'geo_area',
'geo_as_binary',
Expand Down Expand Up @@ -347,6 +348,31 @@ def desc(expr: ir.ColumnExpr | str) -> ir.SortExpr | ops.DeferredSortKey:
return ops.SortKey(expr, ascending=False).to_expr()


def asc(expr: ir.ColumnExpr | str) -> ir.SortExpr | ops.DeferredSortKey:
"""Create a ascending sort key from `asc` or column name.
Parameters
----------
expr
The expression or column name to use for sorting
Examples
--------
>>> import ibis
>>> t = ibis.table([('g', 'string')])
>>> result = t.group_by('g').size('count').sort_by(ibis.asc('count'))
Returns
-------
ops.DeferredSortKey
A deferred sort key
"""
if not isinstance(expr, Expr):
return ops.DeferredSortKey(expr)
else:
return ops.SortKey(expr).to_expr()


def timestamp(
value: str | numbers.Integral,
timezone: str | None = None,
Expand Down
15 changes: 15 additions & 0 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,17 @@ def test_sort_by_desc_deferred_sort_key(table):
assert_equal(result, expected2)


def test_sort_by_asc_deferred_sort_key(table):
result = table.group_by('g').size().sort_by(ibis.asc('count'))

tmp = table.group_by('g').size()
expected = tmp.sort_by(tmp['count'])
expected2 = tmp.sort_by(ibis.asc(tmp['count']))

assert_equal(result, expected)
assert_equal(result, expected2)


def test_slice_convenience(table):
expr = table[:5]
expr2 = table[:5:1]
Expand Down Expand Up @@ -1218,6 +1229,10 @@ def test_sort_by2(table):
result = m.sort_by(ibis.desc(lambda x: x.foo))
expected = m.sort_by(ibis.desc('foo'))
assert_equal(result, expected)

result = m.sort_by(ibis.asc(lambda x: x.foo))
expected = m.sort_by('foo')
assert_equal(result, expected)


def test_projection2(table):
Expand Down

0 comments on commit f907736

Please sign in to comment.