MCPcopy
hub / github.com/pandas-dev/pandas / safe_sort

Function safe_sort

pandas/core/algorithms.py:1438–1557  ·  view source on GitHub ↗

Sort ``values`` and reorder corresponding ``codes``. ``values`` should be unique if ``codes`` is not None. Safe for use with mixed types (int, str), orders ints before strs. Parameters ---------- values : list-like Sequence; must be unique if ``codes`` is not None.

(
    values: Index | ArrayLike,
    codes: npt.NDArray[np.intp] | None = None,
    use_na_sentinel: bool = True,
    assume_unique: bool = False,
    verify: bool = True,
)

Source from the content-addressed store, hash-verified

1436# low-dependency, is used in this module, and used private methods from
1437# this module.
1438def safe_sort(
1439 values: Index | ArrayLike,
1440 codes: npt.NDArray[np.intp] | None = None,
1441 use_na_sentinel: bool = True,
1442 assume_unique: bool = False,
1443 verify: bool = True,
1444) -> AnyArrayLike | tuple[AnyArrayLike, np.ndarray]:
1445 """
1446 Sort ``values`` and reorder corresponding ``codes``.
1447
1448 ``values`` should be unique if ``codes`` is not None.
1449 Safe for use with mixed types (int, str), orders ints before strs.
1450
1451 Parameters
1452 ----------
1453 values : list-like
1454 Sequence; must be unique if ``codes`` is not None.
1455 codes : np.ndarray[intp] or None, default None
1456 Indices to ``values``. All out of bound indices are treated as
1457 "not found" and will be masked with ``-1``.
1458 use_na_sentinel : bool, default True
1459 If True, the sentinel -1 will be used for NaN values. If False,
1460 NaN values will be encoded as non-negative integers and will not drop the
1461 NaN from the uniques of the values.
1462 assume_unique : bool, default False
1463 When True, ``values`` are assumed to be unique, which can speed up
1464 the calculation. Ignored when ``codes`` is None.
1465 verify : bool, default True
1466 Check if codes are out of bound for the values and put out of bound
1467 codes equal to ``-1``. If ``verify=False``, it is assumed there
1468 are no out of bound codes. Ignored when ``codes`` is None.
1469
1470 Returns
1471 -------
1472 ordered : AnyArrayLike
1473 Sorted ``values``
1474 new_codes : ndarray
1475 Reordered ``codes``; returned when ``codes`` is not None.
1476
1477 Raises
1478 ------
1479 TypeError
1480 * If ``values`` is not list-like or if ``codes`` is neither None
1481 nor list-like
1482 * If ``values`` cannot be sorted
1483 ValueError
1484 * If ``codes`` is not None and ``values`` contain duplicates.
1485 """
1486 if not isinstance(values, (np.ndarray, ABCExtensionArray, ABCIndex)):
1487 raise TypeError(
1488 "Only np.ndarray, ExtensionArray, and Index objects are allowed to "
1489 "be passed to safe_sort as values"
1490 )
1491
1492 sorter = None
1493 ordered: AnyArrayLike
1494
1495 if (

Callers 15

safe_sort_indexFunction · 0.90
test_basic_sortMethod · 0.90
test_codesMethod · 0.90
test_mixed_integerMethod · 0.90
test_unsortableMethod · 0.90
test_exceptionsMethod · 0.90
test_extension_arrayMethod · 0.90
test_mixed_str_nullFunction · 0.90

Calls 9

take_ndFunction · 0.90
_sort_mixedFunction · 0.85
_sort_tuplesFunction · 0.85
uniqueFunction · 0.85
_get_hashtable_algoFunction · 0.85
putMethod · 0.80
argsortMethod · 0.45
takeMethod · 0.45
emptyMethod · 0.45

Tested by 15

test_basic_sortMethod · 0.72
test_codesMethod · 0.72
test_mixed_integerMethod · 0.72
test_unsortableMethod · 0.72
test_exceptionsMethod · 0.72
test_extension_arrayMethod · 0.72
test_mixed_str_nullFunction · 0.72