Skip to content

Commit 622a42f

Browse files
authored
REGR: sort index with sliced MultiIndex (#55474)
* REGR: sort index with sliced MultiIndex * fixes
1 parent fd53117 commit 622a42f

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

pandas/core/sorting.py

+6-21
Original file line numberDiff line numberDiff line change
@@ -365,22 +365,15 @@ def lexsort_indexer(
365365
if codes_given:
366366
mask = k == -1
367367
codes = k.copy()
368-
n = len(codes)
369-
mask_n = n
370-
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray,
371-
# ndarray[Any, Any]]" has no attribute "any"
372-
if mask.any(): # type: ignore[union-attr]
373-
n -= 1
368+
# error: Item "ExtensionArray" of "Series | ExtensionArray |
369+
# ndarray[Any, Any]" has no attribute "max"
370+
n = codes.max() + 1 if len(codes) else 0 # type: ignore[union-attr]
374371

375372
else:
376373
cat = Categorical(k, ordered=True)
377374
n = len(cat.categories)
378375
codes = cat.codes.copy()
379376
mask = cat.codes == -1
380-
if mask.any():
381-
mask_n = n + 1
382-
else:
383-
mask_n = n
384377

385378
if order: # ascending
386379
if na_position == "last":
@@ -391,12 +384,6 @@ def lexsort_indexer(
391384
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
392385
# complex, str, bytes]]]"
393386
codes = np.where(mask, n, codes) # type: ignore[arg-type]
394-
elif na_position == "first":
395-
# error: Incompatible types in assignment (expression has type
396-
# "Union[Any, int, ndarray[Any, dtype[signedinteger[Any]]]]",
397-
# variable has type "Union[Series, ExtensionArray, ndarray[Any, Any]]")
398-
# error: Unsupported operand types for + ("ExtensionArray" and "int")
399-
codes += 1 # type: ignore[operator,assignment]
400387
else: # not order means descending
401388
if na_position == "last":
402389
# error: Unsupported operand types for - ("int" and "ExtensionArray")
@@ -406,9 +393,7 @@ def lexsort_indexer(
406393
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float,
407394
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
408395
# complex, str, bytes]]]"
409-
codes = np.where(
410-
mask, n, n - codes - 1 # type: ignore[operator,arg-type]
411-
)
396+
codes = np.where(mask, n, n - codes - 1) # type: ignore[arg-type]
412397
elif na_position == "first":
413398
# error: Unsupported operand types for - ("int" and "ExtensionArray")
414399
# error: Argument 1 to "where" has incompatible type "Union[Any,
@@ -417,9 +402,9 @@ def lexsort_indexer(
417402
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float,
418403
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
419404
# complex, str, bytes]]]"
420-
codes = np.where(mask, 0, n - codes) # type: ignore[operator,arg-type]
405+
codes = np.where(mask, -1, n - codes) # type: ignore[arg-type]
421406

422-
shape.append(mask_n)
407+
shape.append(n + 1)
423408
labels.append(codes)
424409

425410
return indexer_from_factorized(labels, tuple(shape))

0 commit comments

Comments
 (0)