pyarrow.compute.mode

pyarrow.compute.mode(array, /, n=1, *, skip_nulls=True, min_count=0, options=None, memory_pool=None)

Compute the modal (most common) values of a numeric array.

Compute the n most common values and their respective occurrence counts. The output has type struct<mode: T, count: int64>, where T is the input type. The results are ordered by descending count first, and ascending mode when breaking ties. Nulls are ignored. If there are no non-null values in the array, an empty array is returned.

Parameters
arrayArray-like

Argument to compute function.

nint, default 1

Number of distinct most-common values to return.

skip_nullsbool, default True

Whether to skip (ignore) nulls in the input. If False, any null in the input forces the output to null.

min_countint, default 0

Minimum number of non-null values in the input. If the number of non-null values is below min_count, the output is null.

optionspyarrow.compute.ModeOptions, optional

Alternative way of passing options.

memory_poolpyarrow.MemoryPool, optional

If not passed, will allocate memory from the default memory pool.

Examples

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