9
9
from pandas import compat , lib , tslib , _np_version_under1p8
10
10
from pandas .types .cast import _maybe_promote
11
11
from pandas .types .generic import ABCSeries , ABCIndex
12
- from pandas .types .common import (is_integer_dtype ,
12
+ from pandas .types .common import (is_unsigned_integer_dtype ,
13
+ is_signed_integer_dtype ,
14
+ is_integer_dtype ,
13
15
is_int64_dtype ,
14
16
is_categorical_dtype ,
15
17
is_extension_type ,
@@ -490,12 +492,14 @@ def _value_counts_arraylike(values, dropna=True):
490
492
491
493
def duplicated (values , keep = 'first' ):
492
494
"""
493
- Return boolean ndarray denoting duplicate values
495
+ Return boolean ndarray denoting duplicate values.
494
496
495
497
.. versionadded:: 0.19.0
496
498
497
499
Parameters
498
500
----------
501
+ values : ndarray-like
502
+ Array over which to check for duplicate values.
499
503
keep : {'first', 'last', False}, default 'first'
500
504
- ``first`` : Mark duplicates as ``True`` except for the first
501
505
occurrence.
@@ -521,9 +525,12 @@ def duplicated(values, keep='first'):
521
525
elif isinstance (values , (ABCSeries , ABCIndex )):
522
526
values = values .values
523
527
524
- if is_integer_dtype (dtype ):
528
+ if is_signed_integer_dtype (dtype ):
525
529
values = _ensure_int64 (values )
526
530
duplicated = htable .duplicated_int64 (values , keep = keep )
531
+ elif is_unsigned_integer_dtype (dtype ):
532
+ values = _ensure_uint64 (values )
533
+ duplicated = htable .duplicated_uint64 (values , keep = keep )
527
534
elif is_float_dtype (dtype ):
528
535
values = _ensure_float64 (values )
529
536
duplicated = htable .duplicated_float64 (values , keep = keep )
@@ -535,7 +542,19 @@ def duplicated(values, keep='first'):
535
542
536
543
537
544
def mode (values ):
538
- """Returns the mode or mode(s) of the passed Series or ndarray (sorted)"""
545
+ """
546
+ Returns the mode(s) of an array.
547
+
548
+ Parameters
549
+ ----------
550
+ values : array-like
551
+ Array over which to check for duplicate values.
552
+
553
+ Returns
554
+ -------
555
+ mode : Series
556
+ """
557
+
539
558
# must sort because hash order isn't necessarily defined.
540
559
from pandas .core .series import Series
541
560
@@ -547,23 +566,24 @@ def mode(values):
547
566
constructor = Series
548
567
549
568
dtype = values .dtype
550
- if is_integer_dtype (values ):
569
+ if is_signed_integer_dtype (values ):
551
570
values = _ensure_int64 (values )
552
- result = constructor (sorted (htable .mode_int64 (values )), dtype = dtype )
553
-
571
+ result = constructor (np .sort (htable .mode_int64 (values )), dtype = dtype )
572
+ elif is_unsigned_integer_dtype (values ):
573
+ values = _ensure_uint64 (values )
574
+ result = constructor (np .sort (htable .mode_uint64 (values )), dtype = dtype )
554
575
elif issubclass (values .dtype .type , (np .datetime64 , np .timedelta64 )):
555
576
dtype = values .dtype
556
577
values = values .view (np .int64 )
557
- result = constructor (sorted (htable .mode_int64 (values )), dtype = dtype )
558
-
578
+ result = constructor (np .sort (htable .mode_int64 (values )), dtype = dtype )
559
579
elif is_categorical_dtype (values ):
560
580
result = constructor (values .mode ())
561
581
else :
562
582
mask = isnull (values )
563
583
values = _ensure_object (values )
564
584
res = htable .mode_object (values , mask )
565
585
try :
566
- res = sorted (res )
586
+ res = np . sort (res )
567
587
except TypeError as e :
568
588
warn ("Unable to sort modes: %s" % e )
569
589
result = constructor (res , dtype = dtype )
@@ -893,8 +913,10 @@ def _hashtable_algo(f, values, return_dtype=None):
893
913
dtype = values .dtype
894
914
if is_float_dtype (dtype ):
895
915
return f (htable .Float64HashTable , _ensure_float64 )
896
- elif is_integer_dtype (dtype ):
916
+ elif is_signed_integer_dtype (dtype ):
897
917
return f (htable .Int64HashTable , _ensure_int64 )
918
+ elif is_unsigned_integer_dtype (dtype ):
919
+ return f (htable .UInt64HashTable , _ensure_uint64 )
898
920
elif is_datetime64_dtype (dtype ):
899
921
return_dtype = return_dtype or 'M8[ns]'
900
922
return f (htable .Int64HashTable , _ensure_int64 ).view (return_dtype )
0 commit comments