diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 2cb4df7e054fe..e493e5e9d41d3 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 a9a58cddaf222..c13164a4c4f85 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2921,7 +2921,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 8c9747f551c0a..d1a52011a3ad7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5208,7 +5208,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 08ebae7d5c9aa..25c6de3d255e3 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -14,7 +14,6 @@ from pandas.util._decorators import Appender, cache_readonly, doc from pandas.core.dtypes.common import ( - ensure_int64, is_bool_dtype, is_categorical_dtype, is_dtype_equal, @@ -188,7 +187,7 @@ def __contains__(self, key: Any) -> bool: @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)) @@ -587,7 +586,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 cbc0617ae96d3..bdd3afe747d1d 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -2769,7 +2769,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)) @@ -2816,7 +2816,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 8b67788b1a1a1..49ca8f9ad55e9 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -233,8 +233,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/libs/test_lib.py b/pandas/tests/libs/test_lib.py index c9c34916be32b..da3e18c8d9634 100644 --- a/pandas/tests/libs/test_lib.py +++ b/pandas/tests/libs/test_lib.py @@ -50,7 +50,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) @@ -58,7 +58,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) @@ -73,7 +73,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) @@ -86,7 +86,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) @@ -100,7 +100,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) @@ -113,7 +113,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) @@ -125,7 +125,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) @@ -137,7 +137,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]) @@ -150,7 +150,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) @@ -162,7 +162,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) @@ -177,7 +177,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)