Skip to content

Commit

Permalink
CI: 32 bit maybe_indices_to_slice (#37473)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Oct 29, 2020
1 parent 5532ae8 commit 2414c75
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
3 changes: 2 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ from numpy cimport (
float32_t,
float64_t,
int64_t,
intp_t,
ndarray,
uint8_t,
uint64_t,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 11 additions & 11 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2414c75

Please sign in to comment.