Skip to content

Commit f10bc49

Browse files
Backport PR #37473: CI: 32 bit maybe_indices_to_slice (#37503)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 6acf2af commit f10bc49

File tree

7 files changed

+27
-21
lines changed

7 files changed

+27
-21
lines changed

pandas/_libs/lib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ from numpy cimport (
3636
float32_t,
3737
float64_t,
3838
int64_t,
39+
intp_t,
3940
ndarray,
4041
uint8_t,
4142
uint64_t,
@@ -490,7 +491,7 @@ def has_infs_f8(const float64_t[:] arr) -> bool:
490491
return False
491492

492493

493-
def maybe_indices_to_slice(ndarray[int64_t] indices, int max_len):
494+
def maybe_indices_to_slice(ndarray[intp_t] indices, int max_len):
494495
cdef:
495496
Py_ssize_t i, n = len(indices)
496497
int k, vstart, vlast, v

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,9 @@ def __getitem__(self, key):
28812881
indexer = convert_to_index_sliceable(self, key)
28822882
if indexer is not None:
28832883
if isinstance(indexer, np.ndarray):
2884-
indexer = lib.maybe_indices_to_slice(indexer, len(self))
2884+
indexer = lib.maybe_indices_to_slice(
2885+
indexer.astype(np.intp, copy=False), len(self)
2886+
)
28852887
# either we have a slice or we have a string that can be converted
28862888
# to a slice for partial-string date indexing
28872889
return self._slice(indexer, axis=0)

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5094,7 +5094,9 @@ def get_slice_bound(self, label, side: str_t, kind) -> int:
50945094
if is_bool_dtype(slc):
50955095
slc = lib.maybe_booleans_to_slice(slc.view("u1"))
50965096
else:
5097-
slc = lib.maybe_indices_to_slice(slc.astype("i8"), len(self))
5097+
slc = lib.maybe_indices_to_slice(
5098+
slc.astype(np.intp, copy=False), len(self)
5099+
)
50985100
if isinstance(slc, np.ndarray):
50995101
raise KeyError(
51005102
f"Cannot get {side} slice bound for non-unique "

pandas/core/indexes/datetimelike.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pandas.util._decorators import Appender, cache_readonly, doc
1616

1717
from pandas.core.dtypes.common import (
18-
ensure_int64,
1918
is_bool_dtype,
2019
is_dtype_equal,
2120
is_integer,
@@ -181,7 +180,7 @@ def sort_values(self, return_indexer=False, ascending=True, key=None):
181180
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
182181
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
183182
nv.validate_take(tuple(), kwargs)
184-
indices = ensure_int64(indices)
183+
indices = np.asarray(indices, dtype=np.intp)
185184

186185
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
187186
if isinstance(maybe_slice, slice):
@@ -581,7 +580,9 @@ def delete(self, loc):
581580
freq = self.freq
582581
else:
583582
if is_list_like(loc):
584-
loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self))
583+
loc = lib.maybe_indices_to_slice(
584+
np.asarray(loc, dtype=np.intp), len(self)
585+
)
585586
if isinstance(loc, slice) and loc.step in (1, None):
586587
if loc.start in (0, None) or loc.stop in (len(self), None):
587588
freq = self.freq

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@ def get_loc(self, key, method=None):
26882688

26892689
def _maybe_to_slice(loc):
26902690
"""convert integer indexer to boolean mask or slice if possible"""
2691-
if not isinstance(loc, np.ndarray) or loc.dtype != "int64":
2691+
if not isinstance(loc, np.ndarray) or loc.dtype != np.intp:
26922692
return loc
26932693

26942694
loc = lib.maybe_indices_to_slice(loc, len(self))
@@ -2735,7 +2735,7 @@ def _maybe_to_slice(loc):
27352735
stacklevel=10,
27362736
)
27372737

2738-
loc = np.arange(start, stop, dtype="int64")
2738+
loc = np.arange(start, stop, dtype=np.intp)
27392739

27402740
for i, k in enumerate(follow_key, len(lead_key)):
27412741
mask = self.codes[i][loc] == self._get_loc_single_level_index(

pandas/core/internals/managers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ def _rebuild_blknos_and_blklocs(self) -> None:
239239
"""
240240
Update mgr._blknos / mgr._blklocs.
241241
"""
242-
new_blknos = np.empty(self.shape[0], dtype=np.int64)
243-
new_blklocs = np.empty(self.shape[0], dtype=np.int64)
242+
new_blknos = np.empty(self.shape[0], dtype=np.intp)
243+
new_blklocs = np.empty(self.shape[0], dtype=np.intp)
244244
new_blknos.fill(-1)
245245
new_blklocs.fill(-1)
246246

pandas/tests/test_lib.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def test_maybe_indices_to_slice_left_edge(self):
5151
target = np.arange(100)
5252

5353
# slice
54-
indices = np.array([], dtype=np.int64)
54+
indices = np.array([], dtype=np.intp)
5555
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
5656

5757
assert isinstance(maybe_slice, slice)
5858
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
5959

6060
for end in [1, 2, 5, 20, 99]:
6161
for step in [1, 2, 4]:
62-
indices = np.arange(0, end, step, dtype=np.int64)
62+
indices = np.arange(0, end, step, dtype=np.intp)
6363
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
6464

6565
assert isinstance(maybe_slice, slice)
@@ -74,7 +74,7 @@ def test_maybe_indices_to_slice_left_edge(self):
7474

7575
# not slice
7676
for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]:
77-
indices = np.array(case, dtype=np.int64)
77+
indices = np.array(case, dtype=np.intp)
7878
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
7979

8080
assert not isinstance(maybe_slice, slice)
@@ -87,7 +87,7 @@ def test_maybe_indices_to_slice_right_edge(self):
8787
# slice
8888
for start in [0, 2, 5, 20, 97, 98]:
8989
for step in [1, 2, 4]:
90-
indices = np.arange(start, 99, step, dtype=np.int64)
90+
indices = np.arange(start, 99, step, dtype=np.intp)
9191
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
9292

9393
assert isinstance(maybe_slice, slice)
@@ -101,7 +101,7 @@ def test_maybe_indices_to_slice_right_edge(self):
101101
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
102102

103103
# not slice
104-
indices = np.array([97, 98, 99, 100], dtype=np.int64)
104+
indices = np.array([97, 98, 99, 100], dtype=np.intp)
105105
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
106106

107107
assert not isinstance(maybe_slice, slice)
@@ -114,7 +114,7 @@ def test_maybe_indices_to_slice_right_edge(self):
114114
with pytest.raises(IndexError, match=msg):
115115
target[maybe_slice]
116116

117-
indices = np.array([100, 99, 98, 97], dtype=np.int64)
117+
indices = np.array([100, 99, 98, 97], dtype=np.intp)
118118
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
119119

120120
assert not isinstance(maybe_slice, slice)
@@ -126,7 +126,7 @@ def test_maybe_indices_to_slice_right_edge(self):
126126
target[maybe_slice]
127127

128128
for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
129-
indices = np.array(case, dtype=np.int64)
129+
indices = np.array(case, dtype=np.intp)
130130
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
131131

132132
assert not isinstance(maybe_slice, slice)
@@ -138,7 +138,7 @@ def test_maybe_indices_to_slice_both_edges(self):
138138

139139
# slice
140140
for step in [1, 2, 4, 5, 8, 9]:
141-
indices = np.arange(0, 9, step, dtype=np.int64)
141+
indices = np.arange(0, 9, step, dtype=np.intp)
142142
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
143143
assert isinstance(maybe_slice, slice)
144144
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
@@ -151,7 +151,7 @@ def test_maybe_indices_to_slice_both_edges(self):
151151

152152
# not slice
153153
for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
154-
indices = np.array(case, dtype=np.int64)
154+
indices = np.array(case, dtype=np.intp)
155155
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
156156
assert not isinstance(maybe_slice, slice)
157157
tm.assert_numpy_array_equal(maybe_slice, indices)
@@ -163,7 +163,7 @@ def test_maybe_indices_to_slice_middle(self):
163163
# slice
164164
for start, end in [(2, 10), (5, 25), (65, 97)]:
165165
for step in [1, 2, 4, 20]:
166-
indices = np.arange(start, end, step, dtype=np.int64)
166+
indices = np.arange(start, end, step, dtype=np.intp)
167167
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
168168

169169
assert isinstance(maybe_slice, slice)
@@ -178,7 +178,7 @@ def test_maybe_indices_to_slice_middle(self):
178178

179179
# not slice
180180
for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
181-
indices = np.array(case, dtype=np.int64)
181+
indices = np.array(case, dtype=np.intp)
182182
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
183183

184184
assert not isinstance(maybe_slice, slice)

0 commit comments

Comments
 (0)