Skip to content

Commit c47bc50

Browse files
committed
CLN: did not need to convert to index array/slicer as the only
time this happens is when a boolean array comes back from get_loc, means the index is non_monotonic, which is an exception in any event
1 parent 8f3ed7f commit c47bc50

File tree

1 file changed

+8
-45
lines changed

1 file changed

+8
-45
lines changed

pandas/core/index.py

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,10 +1222,7 @@ def slice_locs(self, start=None, end=None):
12221222

12231223
is_unique = self.is_unique
12241224
if start is None:
1225-
if is_unique:
1226-
start_slice = 0
1227-
else:
1228-
start_slice = np.arange(len(self))
1225+
start_slice = 0
12291226
else:
12301227
try:
12311228
start_slice = self.get_loc(start)
@@ -1235,21 +1232,10 @@ def slice_locs(self, start=None, end=None):
12351232
# get_loc will return a boolean array for non_uniques
12361233
# if we are not monotonic
12371234
if isinstance(start_slice,np.ndarray):
1238-
if not self.is_monotonic:
1239-
raise KeyError("cannot peform a slice operation "
1240-
"on a non-unique non-monotonic index")
1241-
start_slice = np.arange(len(self))[start_slice]
1242-
1243-
# select all in the slice + all the rest of the entries
1244-
# to the right
1245-
elif isinstance(start_slice, slice):
1246-
ss = np.arange(start_slice.stop,len(self))
1247-
start_slice = np.arange(len(self))[start_slice]
1248-
start_slice = (Index(ss) | Index(start_slice)).values
1249-
else:
1250-
start_slice = np.arange(start_slice,len(self))
1235+
raise KeyError("cannot peform a slice operation "
1236+
"on a non-unique non-monotonic index")
12511237

1252-
elif isinstance(start_slice, slice):
1238+
if isinstance(start_slice, slice):
12531239
start_slice = start_slice.start
12541240

12551241
except KeyError:
@@ -1259,10 +1245,7 @@ def slice_locs(self, start=None, end=None):
12591245
raise
12601246

12611247
if end is None:
1262-
if is_unique:
1263-
end_slice = len(self)
1264-
else:
1265-
end_slice = np.arange(len(self))
1248+
end_slice = len(self)
12661249
else:
12671250
try:
12681251
end_slice = self.get_loc(end)
@@ -1271,20 +1254,10 @@ def slice_locs(self, start=None, end=None):
12711254

12721255
# get_loc will return a boolean array for non_uniques
12731256
if isinstance(end_slice,np.ndarray):
1274-
if not self.is_monotonic:
1275-
raise KeyError("cannot perform a slice operation "
1276-
"on a non-unique non-monotonic index")
1277-
end_slice = np.arange(len(self))[end_slice]
1278-
1279-
# select all in the slice + all to the left of the entries
1280-
elif isinstance(end_slice, slice):
1281-
es = np.arange(0,end_slice.start)
1282-
end_slice = np.arange(len(self))[end_slice]
1283-
end_slice = (Index(es) | Index(end_slice)).values
1284-
else:
1285-
end_slice = np.arange(0,end_slice+1)
1257+
raise KeyError("cannot perform a slice operation "
1258+
"on a non-unique non-monotonic index")
12861259

1287-
elif isinstance(end_slice, slice):
1260+
if isinstance(end_slice, slice):
12881261
end_slice = end_slice.stop
12891262
else:
12901263
end_slice += 1
@@ -1295,16 +1268,6 @@ def slice_locs(self, start=None, end=None):
12951268
else:
12961269
raise
12971270

1298-
if not is_unique:
1299-
# see if we can convert back to and edge slice
1300-
if len(start_slice) == len(end_slice) and (start_slice == end_slice).all():
1301-
start_slice, end_slice = start_slice[0], start_slice[-1]+1
1302-
# partial slice
1303-
elif (len(start_slice) == start_slice[-1]-start_slice[0]+1) and (
1304-
len(end_slice) == end_slice[-1]-end_slice[0]+1):
1305-
res = (Index(start_slice) & Index(end_slice)).values
1306-
start_slice, end_slice = res[0],res[-1]+1
1307-
13081271
return start_slice, end_slice
13091272

13101273
def delete(self, loc):

0 commit comments

Comments
 (0)