diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 6bf0aba128e39..7127c57defee3 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -36,6 +36,7 @@ from numpy cimport ( float32_t, float64_t, int64_t, + intp_t, ndarray, uint8_t, uint64_t, @@ -490,7 +491,7 @@ def has_infs_f8(const float64_t[:] arr) -> bool: return False -def maybe_indices_to_slice(ndarray[int64_t] indices, int max_len): +def maybe_indices_to_slice(ndarray[intp_t] indices, int max_len): cdef: Py_ssize_t i, n = len(indices) int k, vstart, vlast, v diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5d7f15be6e107..0b2c99ea674c2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2881,7 +2881,9 @@ def __getitem__(self, key): indexer = convert_to_index_sliceable(self, key) if indexer is not None: if isinstance(indexer, np.ndarray): - indexer = lib.maybe_indices_to_slice(indexer, len(self)) + indexer = lib.maybe_indices_to_slice( + indexer.astype(np.intp, copy=False), len(self) + ) # either we have a slice or we have a string that can be converted # to a slice for partial-string date indexing return self._slice(indexer, axis=0) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a18f7bdccd0d0..e4dee2b0a08ce 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5094,7 +5094,9 @@ def get_slice_bound(self, label, side: str_t, kind) -> int: if is_bool_dtype(slc): slc = lib.maybe_booleans_to_slice(slc.view("u1")) else: - slc = lib.maybe_indices_to_slice(slc.astype("i8"), len(self)) + slc = lib.maybe_indices_to_slice( + slc.astype(np.intp, copy=False), len(self) + ) if isinstance(slc, np.ndarray): raise KeyError( f"Cannot get {side} slice bound for non-unique " diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 9b57a25f1b0e9..b30ef37c14b4b 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -15,7 +15,6 @@ from pandas.util._decorators import Appender, cache_readonly, doc from pandas.core.dtypes.common import ( - ensure_int64, is_bool_dtype, is_dtype_equal, is_integer, @@ -181,7 +180,7 @@ def sort_values(self, return_indexer=False, ascending=True, key=None): @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): nv.validate_take(tuple(), kwargs) - indices = ensure_int64(indices) + indices = np.asarray(indices, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(self)) if isinstance(maybe_slice, slice): @@ -581,7 +580,9 @@ def delete(self, loc): freq = self.freq else: if is_list_like(loc): - loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self)) + loc = lib.maybe_indices_to_slice( + np.asarray(loc, dtype=np.intp), len(self) + ) if isinstance(loc, slice) and loc.step in (1, None): if loc.start in (0, None) or loc.stop in (len(self), None): freq = self.freq diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 142e1bbe98d60..b9ba823ca1b0b 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -2688,7 +2688,7 @@ def get_loc(self, key, method=None): def _maybe_to_slice(loc): """convert integer indexer to boolean mask or slice if possible""" - if not isinstance(loc, np.ndarray) or loc.dtype != "int64": + if not isinstance(loc, np.ndarray) or loc.dtype != np.intp: return loc loc = lib.maybe_indices_to_slice(loc, len(self)) @@ -2735,7 +2735,7 @@ def _maybe_to_slice(loc): stacklevel=10, ) - loc = np.arange(start, stop, dtype="int64") + loc = np.arange(start, stop, dtype=np.intp) for i, k in enumerate(follow_key, len(lead_key)): mask = self.codes[i][loc] == self._get_loc_single_level_index( diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 3f3f0c68cb1ed..c9ac9cb0f140a 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -239,8 +239,8 @@ def _rebuild_blknos_and_blklocs(self) -> None: """ Update mgr._blknos / mgr._blklocs. """ - new_blknos = np.empty(self.shape[0], dtype=np.int64) - new_blklocs = np.empty(self.shape[0], dtype=np.int64) + new_blknos = np.empty(self.shape[0], dtype=np.intp) + new_blklocs = np.empty(self.shape[0], dtype=np.intp) new_blknos.fill(-1) new_blklocs.fill(-1) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index b6f59807eaa15..60cbe0d94e734 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -51,7 +51,7 @@ def test_maybe_indices_to_slice_left_edge(self): target = np.arange(100) # slice - indices = np.array([], dtype=np.int64) + indices = np.array([], dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert isinstance(maybe_slice, slice) @@ -59,7 +59,7 @@ def test_maybe_indices_to_slice_left_edge(self): for end in [1, 2, 5, 20, 99]: for step in [1, 2, 4]: - indices = np.arange(0, end, step, dtype=np.int64) + indices = np.arange(0, end, step, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert isinstance(maybe_slice, slice) @@ -74,7 +74,7 @@ def test_maybe_indices_to_slice_left_edge(self): # not slice for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]: - indices = np.array(case, dtype=np.int64) + indices = np.array(case, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice) @@ -87,7 +87,7 @@ def test_maybe_indices_to_slice_right_edge(self): # slice for start in [0, 2, 5, 20, 97, 98]: for step in [1, 2, 4]: - indices = np.arange(start, 99, step, dtype=np.int64) + indices = np.arange(start, 99, step, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert isinstance(maybe_slice, slice) @@ -101,7 +101,7 @@ def test_maybe_indices_to_slice_right_edge(self): tm.assert_numpy_array_equal(target[indices], target[maybe_slice]) # not slice - indices = np.array([97, 98, 99, 100], dtype=np.int64) + indices = np.array([97, 98, 99, 100], dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice) @@ -114,7 +114,7 @@ def test_maybe_indices_to_slice_right_edge(self): with pytest.raises(IndexError, match=msg): target[maybe_slice] - indices = np.array([100, 99, 98, 97], dtype=np.int64) + indices = np.array([100, 99, 98, 97], dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice) @@ -126,7 +126,7 @@ def test_maybe_indices_to_slice_right_edge(self): target[maybe_slice] for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]: - indices = np.array(case, dtype=np.int64) + indices = np.array(case, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice) @@ -138,7 +138,7 @@ def test_maybe_indices_to_slice_both_edges(self): # slice for step in [1, 2, 4, 5, 8, 9]: - indices = np.arange(0, 9, step, dtype=np.int64) + indices = np.arange(0, 9, step, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(target[indices], target[maybe_slice]) @@ -151,7 +151,7 @@ def test_maybe_indices_to_slice_both_edges(self): # not slice for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]: - indices = np.array(case, dtype=np.int64) + indices = np.array(case, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) @@ -163,7 +163,7 @@ def test_maybe_indices_to_slice_middle(self): # slice for start, end in [(2, 10), (5, 25), (65, 97)]: for step in [1, 2, 4, 20]: - indices = np.arange(start, end, step, dtype=np.int64) + indices = np.arange(start, end, step, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert isinstance(maybe_slice, slice) @@ -178,7 +178,7 @@ def test_maybe_indices_to_slice_middle(self): # not slice for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]: - indices = np.array(case, dtype=np.int64) + indices = np.array(case, dtype=np.intp) maybe_slice = lib.maybe_indices_to_slice(indices, len(target)) assert not isinstance(maybe_slice, slice)