Skip to content

Commit edbc6ea

Browse files
authored
CLN: remove unreached boolean-mask case from _preprocess_slice_or_indexer (#40401)
1 parent ae12817 commit edbc6ea

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

pandas/core/internals/concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from pandas import Index
5454

5555

56-
def concatenate_array_managers(
56+
def _concatenate_array_managers(
5757
mgrs_indexers, axes: List[Index], concat_axis: int, copy: bool
5858
) -> Manager:
5959
"""
@@ -111,7 +111,7 @@ def concatenate_managers(
111111
"""
112112
# TODO(ArrayManager) this assumes that all managers are of the same type
113113
if isinstance(mgrs_indexers[0][0], ArrayManager):
114-
return concatenate_array_managers(mgrs_indexers, axes, concat_axis, copy)
114+
return _concatenate_array_managers(mgrs_indexers, axes, concat_axis, copy)
115115

116116
concat_plans = [
117117
_get_mgr_concatenation_plan(mgr, indexers) for mgr, indexers in mgrs_indexers

pandas/core/internals/managers.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pandas.core.dtypes.cast import infer_dtype_from_scalar
3636
from pandas.core.dtypes.common import (
3737
DT64NS_DTYPE,
38+
ensure_int64,
3839
is_dtype_equal,
3940
is_extension_array_dtype,
4041
is_list_like,
@@ -1293,7 +1294,7 @@ def insert(
12931294

12941295
def reindex_indexer(
12951296
self: T,
1296-
new_axis,
1297+
new_axis: Index,
12971298
indexer,
12981299
axis: int,
12991300
fill_value=None,
@@ -1359,7 +1360,10 @@ def reindex_indexer(
13591360
return type(self).from_blocks(new_blocks, new_axes)
13601361

13611362
def _slice_take_blocks_ax0(
1362-
self, slice_or_indexer, fill_value=lib.no_default, only_slice: bool = False
1363+
self,
1364+
slice_or_indexer: Union[slice, np.ndarray],
1365+
fill_value=lib.no_default,
1366+
only_slice: bool = False,
13631367
) -> List[Block]:
13641368
"""
13651369
Slice/take blocks along axis=0.
@@ -1368,7 +1372,7 @@ def _slice_take_blocks_ax0(
13681372
13691373
Parameters
13701374
----------
1371-
slice_or_indexer : slice, ndarray[bool], or list-like of ints
1375+
slice_or_indexer : slice or np.ndarray[int64]
13721376
fill_value : scalar, default lib.no_default
13731377
only_slice : bool, default False
13741378
If True, we always return views on existing arrays, never copies.
@@ -1387,12 +1391,11 @@ def _slice_take_blocks_ax0(
13871391
if self.is_single_block:
13881392
blk = self.blocks[0]
13891393

1390-
if sl_type in ("slice", "mask"):
1394+
if sl_type == "slice":
13911395
# GH#32959 EABlock would fail since we can't make 0-width
13921396
# TODO(EA2D): special casing unnecessary with 2D EAs
13931397
if sllen == 0:
13941398
return []
1395-
# TODO: tests all have isinstance(slobj, slice), other possibilities?
13961399
return [blk.getitem_block(slobj, new_mgr_locs=slice(0, sllen))]
13971400
elif not allow_fill or self.ndim == 1:
13981401
if allow_fill and fill_value is None:
@@ -1418,7 +1421,7 @@ def _slice_take_blocks_ax0(
14181421
)
14191422
]
14201423

1421-
if sl_type in ("slice", "mask"):
1424+
if sl_type == "slice":
14221425
blknos = self.blknos[slobj]
14231426
blklocs = self.blklocs[slobj]
14241427
else:
@@ -1661,9 +1664,6 @@ def get_slice(self, slobj: slice, axis: int = 0) -> SingleBlockManager:
16611664

16621665
blk = self._block
16631666
array = blk._slice(slobj)
1664-
if array.ndim > blk.values.ndim:
1665-
# This will be caught by Series._get_values
1666-
raise ValueError("dimension-expanding indexing not allowed")
16671667
block = blk.make_block_same_class(array, placement=slice(0, len(array)))
16681668
new_index = self.index._getitem_slice(slobj)
16691669
return type(self)(block, new_index)
@@ -1978,10 +1978,6 @@ def _merge_blocks(
19781978

19791979
if can_consolidate:
19801980

1981-
if dtype is None:
1982-
if len({b.dtype for b in blocks}) != 1:
1983-
raise AssertionError("_merge_blocks are invalid!")
1984-
19851981
# TODO: optimization potential in case all mgrs contain slices and
19861982
# combination of those slices is a slice, too.
19871983
new_mgr_locs = np.concatenate([b.mgr_locs.as_array for b in blocks])
@@ -2008,20 +2004,25 @@ def _fast_count_smallints(arr: np.ndarray) -> np.ndarray:
20082004
return np.c_[nz, counts[nz]]
20092005

20102006

2011-
def _preprocess_slice_or_indexer(slice_or_indexer, length: int, allow_fill: bool):
2007+
def _preprocess_slice_or_indexer(
2008+
slice_or_indexer: Union[slice, np.ndarray], length: int, allow_fill: bool
2009+
):
20122010
if isinstance(slice_or_indexer, slice):
20132011
return (
20142012
"slice",
20152013
slice_or_indexer,
20162014
libinternals.slice_len(slice_or_indexer, length),
20172015
)
2018-
elif (
2019-
isinstance(slice_or_indexer, np.ndarray) and slice_or_indexer.dtype == np.bool_
2020-
):
2021-
return "mask", slice_or_indexer, slice_or_indexer.sum()
20222016
else:
2017+
if (
2018+
not isinstance(slice_or_indexer, np.ndarray)
2019+
or slice_or_indexer.dtype.kind != "i"
2020+
):
2021+
dtype = getattr(slice_or_indexer, "dtype", None)
2022+
raise TypeError(type(slice_or_indexer), dtype)
2023+
20232024
# TODO: np.intp?
2024-
indexer = np.asanyarray(slice_or_indexer, dtype=np.int64)
2025+
indexer = ensure_int64(slice_or_indexer)
20252026
if not allow_fill:
20262027
indexer = maybe_convert_indices(indexer, length)
20272028
return "fancy", indexer, len(indexer)

pandas/tests/internals/test_internals.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ def assert_reindex_indexer_is_ok(mgr, axis, new_labels, indexer, fill_value):
931931
tm.assert_index_equal(reindexed.axes[axis], new_labels)
932932

933933
for ax in range(mgr.ndim):
934-
assert_reindex_indexer_is_ok(mgr, ax, Index([]), [], fill_value)
934+
assert_reindex_indexer_is_ok(
935+
mgr, ax, Index([]), np.array([], dtype=np.intp), fill_value
936+
)
935937
assert_reindex_indexer_is_ok(
936938
mgr, ax, mgr.axes[ax], np.arange(mgr.shape[ax]), fill_value
937939
)
@@ -949,22 +951,26 @@ def assert_reindex_indexer_is_ok(mgr, axis, new_labels, indexer, fill_value):
949951
mgr, ax, mgr.axes[ax], np.arange(mgr.shape[ax])[::-1], fill_value
950952
)
951953
assert_reindex_indexer_is_ok(
952-
mgr, ax, Index(["foo", "bar", "baz"]), [0, 0, 0], fill_value
954+
mgr, ax, Index(["foo", "bar", "baz"]), np.array([0, 0, 0]), fill_value
953955
)
954956
assert_reindex_indexer_is_ok(
955-
mgr, ax, Index(["foo", "bar", "baz"]), [-1, 0, -1], fill_value
957+
mgr, ax, Index(["foo", "bar", "baz"]), np.array([-1, 0, -1]), fill_value
956958
)
957959
assert_reindex_indexer_is_ok(
958960
mgr,
959961
ax,
960962
Index(["foo", mgr.axes[ax][0], "baz"]),
961-
[-1, -1, -1],
963+
np.array([-1, -1, -1]),
962964
fill_value,
963965
)
964966

965967
if mgr.shape[ax] >= 3:
966968
assert_reindex_indexer_is_ok(
967-
mgr, ax, Index(["foo", "bar", "baz"]), [0, 1, 2], fill_value
969+
mgr,
970+
ax,
971+
Index(["foo", "bar", "baz"]),
972+
np.array([0, 1, 2]),
973+
fill_value,
968974
)
969975

970976

0 commit comments

Comments
 (0)