Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions python/pyarrow/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ def sum(array):
return call_function('sum', [array])


def mode(array):
"""
Return the mode (most common value) of a passed numerical
(chunked) array. If there is more than one such value, only
the smallest is returned.

Parameters
----------
array : pyarrow.Array or pyarrow.ChunkedArray

Returns
-------
mode : pyarrow.StructScalar

Examples
--------
>>> import pyarrow as pa
>>> import pyarrow.compute as pc
>>> arr = pa.array([1, 1, 2, 2, 3, 2, 2, 2])
>>> pc.mode(arr)
<pyarrow.StructScalar: {'mode': 2, 'count': 5}>

"""
return call_function("mode", [array])


def filter(data, mask, null_selection_behavior='drop'):
"""
Select values (or records) from array- or table-like data given boolean
Expand Down
25 changes: 25 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ def test_sum_chunked_array(arrow_type):
assert pc.sum(arr).as_py() is None # noqa: E711


def test_mode_array():
# ARROW-9917

arr = pa.array([1, 1, 3, 4, 3, 5], type='int64')
expected = {"mode": 1, "count": 2}
assert pc.mode(arr).as_py() == {"mode": 1, "count": 2}

arr = pa.array([], type='int64')
expected = {"mode": None, "count": None}
assert pc.mode(arr).as_py() == expected


def test_mode_chunked_array():
# ARROW-9917

arr = pa.chunked_array([pa.array([1, 1, 3, 4, 3, 5], type='int64')])
expected = {"mode": 1, "count": 2}
assert pc.mode(arr).as_py() == expected

arr = pa.chunked_array((), type='int64')
expected = {"mode": None, "count": None}
assert arr.num_chunks == 0
assert pc.mode(arr).as_py() == expected


def test_match_substring():
arr = pa.array(["ab", "abc", "ba", None])
result = pc.match_substring(arr, "ab")
Expand Down