16
16
from pandas .core .config import get_option
17
17
from pandas .core import format as fmt
18
18
19
-
20
19
def _cat_compare_op (op ):
21
20
def f (self , other ):
22
21
if isinstance (other , (Categorical , np .ndarray )):
@@ -45,16 +44,6 @@ def _maybe_to_categorical(array):
45
44
return array
46
45
47
46
48
- def _get_codes_for_values (values , levels ):
49
- from pandas .core .algorithms import _get_data_algo , _hashtables
50
- if values .dtype != levels .dtype :
51
- values = com ._ensure_object (values )
52
- levels = com ._ensure_object (levels )
53
- (hash_klass , vec_klass ), vals = _get_data_algo (values , _hashtables )
54
- t = hash_klass (len (levels ))
55
- t .map_locations (levels )
56
- return com ._ensure_platform_int (t .lookup (values ))
57
-
58
47
_codes_doc = """The level codes of this categorical.
59
48
60
49
Level codes are an array if integer which are the positions of the real
@@ -484,7 +473,7 @@ def argsort(self, ascending=True, **kwargs):
484
473
result = result [::- 1 ]
485
474
return result
486
475
487
- def order (self , inplace = False , ascending = True , ** kwargs ):
476
+ def order (self , inplace = False , ascending = True , na_position = 'last' , ** kwargs ):
488
477
""" Sorts the Category by level value returning a new Categorical by default.
489
478
490
479
Only ordered Categoricals can be sorted!
@@ -495,11 +484,11 @@ def order(self, inplace=False, ascending=True, **kwargs):
495
484
----------
496
485
ascending : boolean, default True
497
486
Sort ascending. Passing False sorts descending
487
+ inplace : boolean, default False
488
+ Do operation in place.
498
489
na_position : {'first', 'last'} (optional, default='last')
499
490
'first' puts NaNs at the beginning
500
491
'last' puts NaNs at the end
501
- inplace : boolean, default False
502
- Do operation in place.
503
492
504
493
Returns
505
494
-------
@@ -511,18 +500,22 @@ def order(self, inplace=False, ascending=True, **kwargs):
511
500
"""
512
501
if not self .ordered :
513
502
raise TypeError ("Categorical not ordered" )
514
- _sorted = np .sort (self ._codes .copy ())
503
+ if na_position not in ['last' ,'first' ]:
504
+ raise ValueError ('invalid na_position: {!r}' .format (na_position ))
505
+
506
+ codes = np .sort (self ._codes .copy ())
515
507
if not ascending :
516
- _sorted = _sorted [::- 1 ]
508
+ codes = codes [::- 1 ]
509
+
517
510
if inplace :
518
- self ._codes = _sorted
511
+ self ._codes = codes
519
512
return
520
513
else :
521
- return Categorical (values = _sorted ,levels = self .levels , ordered = self .ordered ,
514
+ return Categorical (values = codes ,levels = self .levels , ordered = self .ordered ,
522
515
name = self .name , fastpath = True )
523
516
524
517
525
- def sort (self , inplace = True , ascending = True , ** kwargs ):
518
+ def sort (self , inplace = True , ascending = True , na_position = 'last' , ** kwargs ):
526
519
""" Sorts the Category inplace by level value.
527
520
528
521
Only ordered Categoricals can be sorted!
@@ -533,11 +526,11 @@ def sort(self, inplace=True, ascending=True, **kwargs):
533
526
----------
534
527
ascending : boolean, default True
535
528
Sort ascending. Passing False sorts descending
529
+ inplace : boolean, default False
530
+ Do operation in place.
536
531
na_position : {'first', 'last'} (optional, default='last')
537
532
'first' puts NaNs at the beginning
538
533
'last' puts NaNs at the end
539
- inplace : boolean, default False
540
- Do operation in place.
541
534
542
535
Returns
543
536
-------
@@ -932,3 +925,20 @@ def describe(self):
932
925
result .index .name = 'levels'
933
926
result .columns = ['counts' ,'freqs' ]
934
927
return result
928
+
929
+ ##### utility routines #####
930
+
931
+ def _get_codes_for_values (values , levels ):
932
+ """"
933
+ utility routine to turn values into codes given the specified levels
934
+ """
935
+
936
+ from pandas .core .algorithms import _get_data_algo , _hashtables
937
+ if values .dtype != levels .dtype :
938
+ values = com ._ensure_object (values )
939
+ levels = com ._ensure_object (levels )
940
+ (hash_klass , vec_klass ), vals = _get_data_algo (values , _hashtables )
941
+ t = hash_klass (len (levels ))
942
+ t .map_locations (levels )
943
+ return com ._ensure_platform_int (t .lookup (values ))
944
+
0 commit comments