arkouda.numpy.sorting

Attributes

Functions

argsort(→ arkouda.numpy.pdarrayclass.pdarray)

Return the permutation (indices) that sorts the array.

coargsort(→ arkouda.numpy.pdarrayclass.pdarray)

Return the permutation that groups the rows (left-to-right), if the

searchsorted(→ Union[int, ...)

Find indices where elements should be inserted to maintain order.

sort(→ arkouda.numpy.pdarrayclass.pdarray)

Return a sorted copy of the array. Only sorts numeric arrays;

Module Contents

arkouda.numpy.sorting.SortingAlgorithm
arkouda.numpy.sorting.argsort(pda: arkouda.numpy.pdarrayclass.pdarray | arkouda.numpy.strings.Strings | arkouda.pandas.categorical.Categorical, algorithm: SortingAlgorithm = SortingAlgorithm.RadixSortLSD, axis: arkouda.numpy.dtypes.int_scalars = 0, ascending: bool = True) arkouda.numpy.pdarrayclass.pdarray[source]

Return the permutation (indices) that sorts the array.

Parameters:
  • pda (pdarray, Strings, or Categorical) – The array to sort (supported: int64, uint64, float64 for pdarray).

  • algorithm (SortingAlgorithm, default SortingAlgorithm.RadixSortLSD) – The algorithm to use for sorting.

  • axis (int, default 0) – Axis to sort along. Negative values are normalized against the array rank. For 1D types (Strings, Categorical), must be 0.

  • ascending (bool, default True) – Sort order.

Returns:

Indices such that pda[indices] is sorted.

Return type:

pdarray

Raises:
  • TypeError – If pda is not a pdarray, Strings, or Categorical.

  • ValueError – If axis is out of bounds.

See also

coargsort

Notes

Uses a least-significant-digit radix sort, which is stable and resilient to non-uniformity in data but communication intensive.

Examples

>>> import arkouda as ak
>>> a = ak.randint(0, 10, 10, seed=1)
>>> a
array([7 9 5 1 4 1 8 5 5 0])
>>> perm = ak.argsort(a)
>>> a[perm]
array([0 1 1 4 5 5 5 7 8 9])
>>> ak.argsort(a, ak.sorting.SortingAlgorithm["RadixSortLSD"])
array([9 3 5 4 2 7 8 0 6 1])
>>> ak.argsort(a, ak.sorting.SortingAlgorithm["TwoArrayRadixSort"])
array([9 3 5 4 2 7 8 0 6 1])
>>> ak.argsort(a, ascending=False)
array([1 6 0 8 7 2 4 5 3 9])
arkouda.numpy.sorting.coargsort(arrays: Sequence[arkouda.numpy.strings.Strings | arkouda.numpy.pdarrayclass.pdarray | arkouda.pandas.categorical.Categorical], algorithm: SortingAlgorithm = SortingAlgorithm.RadixSortLSD, ascending: bool = True) arkouda.numpy.pdarrayclass.pdarray[source]

Return the permutation that groups the rows (left-to-right), if the input arrays are treated as columns. The permutation sorts numeric columns, but not Strings or Categoricals — those are grouped, not ordered.

Parameters:
  • arrays (Sequence of Strings, pdarray, or Categorical) – The columns (int64, uint64, float64, Strings, or Categorical) to sort by row.

  • algorithm (SortingAlgorithm, default=SortingAlgorithm.RadixSortLSD) – The algorithm to be used for sorting the arrays.

  • ascending (bool, default=True) – Whether to sort in ascending order. Ignored when arrays have ndim > 1.

Returns:

The indices that permute the rows into grouped order.

Return type:

pdarray

Raises:

ValueError – If the inputs are not all the same size or not valid array types.

See also

argsort

Notes

Uses a least-significant-digit radix sort, which is stable and resilient to non-uniformity in data but communication intensive. Starts with the last array and moves forward.

For Strings, sorting is based on a hash. This ensures grouping of identical strings, but not lexicographic order. For Categoricals, sorting is based on the internal codes.

Examples

>>> import arkouda as ak
>>> a = ak.array([0, 1, 0, 1])
>>> b = ak.array([1, 1, 0, 0])
>>> perm = ak.coargsort([a, b])
>>> perm
array([2 0 3 1])
>>> a[perm]
array([0 0 1 1])
>>> b[perm]
array([0 1 0 1])
arkouda.numpy.sorting.searchsorted(a: arkouda.numpy.pdarrayclass.pdarray, v: arkouda.numpy.dtypes.int_scalars | arkouda.numpy.dtypes.float64 | arkouda.numpy.dtypes.bigint | arkouda.numpy.pdarrayclass.pdarray, side: Literal['left', 'right'] = 'left', x2_sorted: bool = False) int | arkouda.numpy.pdarrayclass.pdarray[source]

Find indices where elements should be inserted to maintain order.

Find the indices into a sorted array a such that, if the corresponding elements in v were inserted before the indices, the order of a would be preserved.

Parameters:
  • a (pdarray) – 1-D input array. Must be sorted in ascending order. sorter is not currently supported.

  • v (int_scalars, float64, bigint, or pdarray) – Values to insert into a. Can be a scalar or array-like.

  • side ({'left', 'right'}, default='left') – If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index.

  • x2_sorted (bool, default=False) – If True, assumes that v (x2) is already sorted in ascending order. This can improve performance for large, sorted search arrays. If False, no assumption is made about the order of v.

Returns:

indices – If v is an array, returns an array of insertion points with the same shape. If v is a scalar, returns a single integer index.

Return type:

int or pdarray

Raises:
  • ValueError – If a has more than one dimension.

  • TypeError – If a has an unsupported dtype (i.e., not int64, uint64, bigint, or float64). If the dtype of a and v does not match

Examples

>>> import arkouda as ak
>>> a = ak.array([11, 12, 13, 14, 15])
>>> ak.searchsorted(a, 13)
2
>>> ak.searchsorted(a, 13, side='right')
3
>>> v = ak.array([-10, 20, 12, 13])
>>> ak.searchsorted(a, v)
array([0 5 1 2])
>>> v_sorted = ak.array([-10, 12, 13, 20])
>>> ak.searchsorted(a, v_sorted, x2_sorted=True)
array([0 1 2 5])
arkouda.numpy.sorting.sort(pda: arkouda.numpy.pdarrayclass.pdarray, algorithm: SortingAlgorithm = SortingAlgorithm.RadixSortLSD, axis: arkouda.numpy.dtypes.int_scalars = -1) arkouda.numpy.pdarrayclass.pdarray[source]

Return a sorted copy of the array. Only sorts numeric arrays; for Strings, use argsort.

Parameters:
  • pda (pdarray) – The array to sort (int64, uint64, or float64)

  • algorithm (SortingAlgorithm, default=SortingAlgorithm.RadixSortLSD) – The algorithm to be used for sorting the arrays.

  • axis (int_scalars, default=-1) – The axis to sort over. Setting to -1 means that it will sort over axis = ndim - 1.

Returns:

The sorted copy of pda

Return type:

pdarray

Raises:
  • TypeError – Raised if the parameter is not a pdarray

  • ValueError – Raised if sort attempted on a pdarray with an unsupported dtype such as bool

See also

argsort

Notes

Uses a least-significant-digit radix sort, which is stable and resilient to non-uniformity in data but communication intensive.

Examples

>>> import arkouda as ak
>>> a = ak.randint(0, 10, 10, seed=1)
>>> a
array([7 9 5 1 4 1 8 5 5 0])
>>> sorted = ak.sort(a)
>>> sorted
array([0 1 1 4 5 5 5 7 8 9])