| 639 | |
| 640 | |
| 641 | def _factorize_given_labels(data: np.ndarray, labels: np.ndarray) -> np.ndarray: |
| 642 | # Copied from flox |
| 643 | sorter = np.argsort(labels) |
| 644 | is_sorted = array_all(sorter == np.arange(sorter.size)) |
| 645 | codes = np.searchsorted(labels, data, sorter=sorter) |
| 646 | mask = ~np.isin(data, labels) | isnull(data) | (codes == len(labels)) |
| 647 | # codes is the index in to the sorted array. |
| 648 | # if we didn't want sorting, unsort it back |
| 649 | if not is_sorted: |
| 650 | codes[codes == len(labels)] = -1 |
| 651 | codes = sorter[(codes,)] |
| 652 | codes[mask] = -1 |
| 653 | return codes |
| 654 | |
| 655 | |
| 656 | def unique_value_groups( |