diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index accb577d5345c..1842e50c9ba7f 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -234,7 +234,17 @@ def _make_selectors(self) -> None: self.group_index = comp_index self.mask = mask if self.sort: - self.compressor = comp_index.searchsorted(np.arange(ngroups)) + import sys + + # GH 63314: avoid searchsorted bug with py3.14 + numpy < 2.0 + numpy_major = int(np.__version__.split(".")[0]) + has_searchsorted_bug = sys.version_info >= (3, 14) and numpy_major < 2 + + if has_searchsorted_bug: + # use manual approach instead of buggy searchsorted + self.compressor = np.sort(np.unique(comp_index, return_index=True)[1]) + else: + self.compressor = comp_index.searchsorted(np.arange(ngroups)) else: self.compressor = np.sort(np.unique(comp_index, return_index=True)[1])