Skip to content

Commit 7431f77

Browse files
jbrockmendelukarroum
authored andcommitted
CI: 32 bit maybe_indices_to_slice (pandas-dev#37473)
1 parent dd8f701 commit 7431f77

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
@@ -2921,7 +2921,9 @@ def __getitem__(self, key):
29212921
indexer = convert_to_index_sliceable(self, key)
29222922
if indexer is not None:
29232923
if isinstance(indexer, np.ndarray):
2924-
indexer = lib.maybe_indices_to_slice(indexer, len(self))
2924+
indexer = lib.maybe_indices_to_slice(
2925+
indexer.astype(np.intp, copy=False), len(self)
2926+
)
29252927
# either we have a slice or we have a string that can be converted
29262928
# to a slice for partial-string date indexing
29272929
return self._slice(indexer, axis=0)

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5208,7 +5208,9 @@ def get_slice_bound(self, label, side: str_t, kind) -> int:
52085208
if is_bool_dtype(slc):
52095209
slc = lib.maybe_booleans_to_slice(slc.view("u1"))
52105210
else:
5211-
slc = lib.maybe_indices_to_slice(slc.astype("i8"), len(self))
5211+
slc = lib.maybe_indices_to_slice(
5212+
slc.astype(np.intp, copy=False), len(self)
5213+
)
52125214
if isinstance(slc, np.ndarray):
52135215
raise KeyError(
52145216
f"Cannot get {side} slice bound for non-unique "

pandas/core/indexes/datetimelike.py

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

1616
from pandas.core.dtypes.common import (
17-
ensure_int64,
1817
is_bool_dtype,
1918
is_categorical_dtype,
2019
is_dtype_equal,
@@ -188,7 +187,7 @@ def __contains__(self, key: Any) -> bool:
188187
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
189188
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
190189
nv.validate_take(tuple(), kwargs)
191-
indices = ensure_int64(indices)
190+
indices = np.asarray(indices, dtype=np.intp)
192191

193192
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
194193

@@ -587,7 +586,9 @@ def delete(self, loc):
587586
freq = self.freq
588587
else:
589588
if is_list_like(loc):
590-
loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self))
589+
loc = lib.maybe_indices_to_slice(
590+
np.asarray(loc, dtype=np.intp), len(self)
591+
)
591592
if isinstance(loc, slice) and loc.step in (1, None):
592593
if loc.start in (0, None) or loc.stop in (len(self), None):
593594
freq = self.freq

pandas/core/indexes/multi.py

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

27702770
def _maybe_to_slice(loc):
27712771
"""convert integer indexer to boolean mask or slice if possible"""
2772-
if not isinstance(loc, np.ndarray) or loc.dtype != "int64":
2772+
if not isinstance(loc, np.ndarray) or loc.dtype != np.intp:
27732773
return loc
27742774

27752775
loc = lib.maybe_indices_to_slice(loc, len(self))
@@ -2816,7 +2816,7 @@ def _maybe_to_slice(loc):
28162816
stacklevel=10,
28172817
)
28182818

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

28212821
for i, k in enumerate(follow_key, len(lead_key)):
28222822
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
@@ -233,8 +233,8 @@ def _rebuild_blknos_and_blklocs(self) -> None:
233233
"""
234234
Update mgr._blknos / mgr._blklocs.
235235
"""
236-
new_blknos = np.empty(self.shape[0], dtype=np.int64)
237-
new_blklocs = np.empty(self.shape[0], dtype=np.int64)
236+
new_blknos = np.empty(self.shape[0], dtype=np.intp)
237+
new_blklocs = np.empty(self.shape[0], dtype=np.intp)
238238
new_blknos.fill(-1)
239239
new_blklocs.fill(-1)
240240

pandas/tests/libs/test_lib.py

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

5252
# slice
53-
indices = np.array([], dtype=np.int64)
53+
indices = np.array([], dtype=np.intp)
5454
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
5555

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

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

6464
assert isinstance(maybe_slice, slice)
@@ -73,7 +73,7 @@ def test_maybe_indices_to_slice_left_edge(self):
7373

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

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

9292
assert isinstance(maybe_slice, slice)
@@ -100,7 +100,7 @@ def test_maybe_indices_to_slice_right_edge(self):
100100
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
101101

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

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

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

119119
assert not isinstance(maybe_slice, slice)
@@ -125,7 +125,7 @@ def test_maybe_indices_to_slice_right_edge(self):
125125
target[maybe_slice]
126126

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

131131
assert not isinstance(maybe_slice, slice)
@@ -137,7 +137,7 @@ def test_maybe_indices_to_slice_both_edges(self):
137137

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

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

168168
assert isinstance(maybe_slice, slice)
@@ -177,7 +177,7 @@ def test_maybe_indices_to_slice_middle(self):
177177

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

183183
assert not isinstance(maybe_slice, slice)

0 commit comments

Comments
 (0)