35
35
from pandas .core .dtypes .cast import infer_dtype_from_scalar
36
36
from pandas .core .dtypes .common import (
37
37
DT64NS_DTYPE ,
38
+ ensure_int64 ,
38
39
is_dtype_equal ,
39
40
is_extension_array_dtype ,
40
41
is_list_like ,
@@ -1293,7 +1294,7 @@ def insert(
1293
1294
1294
1295
def reindex_indexer (
1295
1296
self : T ,
1296
- new_axis ,
1297
+ new_axis : Index ,
1297
1298
indexer ,
1298
1299
axis : int ,
1299
1300
fill_value = None ,
@@ -1359,7 +1360,10 @@ def reindex_indexer(
1359
1360
return type (self ).from_blocks (new_blocks , new_axes )
1360
1361
1361
1362
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 ,
1363
1367
) -> List [Block ]:
1364
1368
"""
1365
1369
Slice/take blocks along axis=0.
@@ -1368,7 +1372,7 @@ def _slice_take_blocks_ax0(
1368
1372
1369
1373
Parameters
1370
1374
----------
1371
- slice_or_indexer : slice, ndarray[bool], or list-like of ints
1375
+ slice_or_indexer : slice or np. ndarray[int64]
1372
1376
fill_value : scalar, default lib.no_default
1373
1377
only_slice : bool, default False
1374
1378
If True, we always return views on existing arrays, never copies.
@@ -1387,12 +1391,11 @@ def _slice_take_blocks_ax0(
1387
1391
if self .is_single_block :
1388
1392
blk = self .blocks [0 ]
1389
1393
1390
- if sl_type in ( "slice" , "mask" ) :
1394
+ if sl_type == "slice" :
1391
1395
# GH#32959 EABlock would fail since we can't make 0-width
1392
1396
# TODO(EA2D): special casing unnecessary with 2D EAs
1393
1397
if sllen == 0 :
1394
1398
return []
1395
- # TODO: tests all have isinstance(slobj, slice), other possibilities?
1396
1399
return [blk .getitem_block (slobj , new_mgr_locs = slice (0 , sllen ))]
1397
1400
elif not allow_fill or self .ndim == 1 :
1398
1401
if allow_fill and fill_value is None :
@@ -1418,7 +1421,7 @@ def _slice_take_blocks_ax0(
1418
1421
)
1419
1422
]
1420
1423
1421
- if sl_type in ( "slice" , "mask" ) :
1424
+ if sl_type == "slice" :
1422
1425
blknos = self .blknos [slobj ]
1423
1426
blklocs = self .blklocs [slobj ]
1424
1427
else :
@@ -1661,9 +1664,6 @@ def get_slice(self, slobj: slice, axis: int = 0) -> SingleBlockManager:
1661
1664
1662
1665
blk = self ._block
1663
1666
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" )
1667
1667
block = blk .make_block_same_class (array , placement = slice (0 , len (array )))
1668
1668
new_index = self .index ._getitem_slice (slobj )
1669
1669
return type (self )(block , new_index )
@@ -1978,10 +1978,6 @@ def _merge_blocks(
1978
1978
1979
1979
if can_consolidate :
1980
1980
1981
- if dtype is None :
1982
- if len ({b .dtype for b in blocks }) != 1 :
1983
- raise AssertionError ("_merge_blocks are invalid!" )
1984
-
1985
1981
# TODO: optimization potential in case all mgrs contain slices and
1986
1982
# combination of those slices is a slice, too.
1987
1983
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:
2008
2004
return np .c_ [nz , counts [nz ]]
2009
2005
2010
2006
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
+ ):
2012
2010
if isinstance (slice_or_indexer , slice ):
2013
2011
return (
2014
2012
"slice" ,
2015
2013
slice_or_indexer ,
2016
2014
libinternals .slice_len (slice_or_indexer , length ),
2017
2015
)
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 ()
2022
2016
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
+
2023
2024
# TODO: np.intp?
2024
- indexer = np . asanyarray (slice_or_indexer , dtype = np . int64 )
2025
+ indexer = ensure_int64 (slice_or_indexer )
2025
2026
if not allow_fill :
2026
2027
indexer = maybe_convert_indices (indexer , length )
2027
2028
return "fancy" , indexer , len (indexer )
0 commit comments