raccoon.sort_utils module

Utility functions for sorting and dealing with sorted Series and DataFrames

raccoon.sort_utils.sorted_exists(values: list[Any], x: Any) tuple[bool, int][source]

For list, values, returns the insert position for item x and whether the item already exists in the list. This allows one function call to return either the index to overwrite an existing value in the list, or the index to insert a new item in the list and keep the list in sorted order.

Parameters:
  • values – list

  • x – item

Returns:

(exists, index) tuple

raccoon.sort_utils.sorted_index(values: list[Any], x: Any) int[source]

For list, values, returns the index location of element x. If x does not exist will raise an error.

Parameters:
  • values – list

  • x – item

Returns:

integer index

raccoon.sort_utils.sorted_list_indexes(list_to_sort: list[T], key: Callable[[T], Any] | None = None, reverse: bool = False) list[int][source]

Sorts a list but returns the order of the index values of the list for the sort and not the values themselves. For example is the list provided is [‘b’, ‘a’, ‘c’] then the result will be [2, 1, 3]

Parameters:
  • list_to_sort – list to sort

  • key – if not None then a function of one argument that is used to extract a comparison key from each list element

  • reverse – if True then the list elements are sorted as if each comparison were reversed.

Returns:

list of sorted index values