@@ -365,22 +365,15 @@ def lexsort_indexer(
365
365
if codes_given :
366
366
mask = k == - 1
367
367
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]
374
371
375
372
else :
376
373
cat = Categorical (k , ordered = True )
377
374
n = len (cat .categories )
378
375
codes = cat .codes .copy ()
379
376
mask = cat .codes == - 1
380
- if mask .any ():
381
- mask_n = n + 1
382
- else :
383
- mask_n = n
384
377
385
378
if order : # ascending
386
379
if na_position == "last" :
@@ -391,12 +384,6 @@ def lexsort_indexer(
391
384
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
392
385
# complex, str, bytes]]]"
393
386
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]
400
387
else : # not order means descending
401
388
if na_position == "last" :
402
389
# error: Unsupported operand types for - ("int" and "ExtensionArray")
@@ -406,9 +393,7 @@ def lexsort_indexer(
406
393
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float,
407
394
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
408
395
# 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]
412
397
elif na_position == "first" :
413
398
# error: Unsupported operand types for - ("int" and "ExtensionArray")
414
399
# error: Argument 1 to "where" has incompatible type "Union[Any,
@@ -417,9 +402,9 @@ def lexsort_indexer(
417
402
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float,
418
403
# complex, str, bytes, _NestedSequence[Union[bool, int, float,
419
404
# 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]
421
406
422
- shape .append (mask_n )
407
+ shape .append (n + 1 )
423
408
labels .append (codes )
424
409
425
410
return indexer_from_factorized (labels , tuple (shape ))
0 commit comments