From f7b4476d61bb2bfe32b9e651ec68b5d6d646a271 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Mon, 4 Dec 2023 13:36:18 -0800 Subject: [PATCH] Address violation fix --- pandas/core/methods/selectn.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/methods/selectn.py b/pandas/core/methods/selectn.py index 843c54de25bc9..a2f8ca94134b8 100644 --- a/pandas/core/methods/selectn.py +++ b/pandas/core/methods/selectn.py @@ -140,7 +140,10 @@ def compute(self, method: str) -> Series: # arr passed into kth_smallest must be contiguous. We copy # here because kth_smallest will modify its input # avoid OOB access with kth_smallest_c when n <= 0 - kth_val = libalgos.kth_smallest(arr.copy(order="C"), max(n - 1, 0)) + if len(arr) > 0: + kth_val = libalgos.kth_smallest(arr.copy(order="C"), n - 1) + else: + kth_val = np.nan (ns,) = np.nonzero(arr <= kth_val) inds = ns[arr[ns].argsort(kind="mergesort")]