1616from pandas .core .config import get_option
1717from pandas .core import format as fmt
1818
19-
2019def _cat_compare_op (op ):
2120 def f (self , other ):
2221 if isinstance (other , (Categorical , np .ndarray )):
@@ -45,16 +44,6 @@ def _maybe_to_categorical(array):
4544 return array
4645
4746
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-
5847_codes_doc = """The level codes of this categorical.
5948
6049Level codes are an array if integer which are the positions of the real
@@ -484,7 +473,7 @@ def argsort(self, ascending=True, **kwargs):
484473 result = result [::- 1 ]
485474 return result
486475
487- def order (self , inplace = False , ascending = True , ** kwargs ):
476+ def order (self , inplace = False , ascending = True , na_position = 'last' , ** kwargs ):
488477 """ Sorts the Category by level value returning a new Categorical by default.
489478
490479 Only ordered Categoricals can be sorted!
@@ -495,11 +484,11 @@ def order(self, inplace=False, ascending=True, **kwargs):
495484 ----------
496485 ascending : boolean, default True
497486 Sort ascending. Passing False sorts descending
487+ inplace : boolean, default False
488+ Do operation in place.
498489 na_position : {'first', 'last'} (optional, default='last')
499490 'first' puts NaNs at the beginning
500491 'last' puts NaNs at the end
501- inplace : boolean, default False
502- Do operation in place.
503492
504493 Returns
505494 -------
@@ -511,18 +500,22 @@ def order(self, inplace=False, ascending=True, **kwargs):
511500 """
512501 if not self .ordered :
513502 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 ())
515507 if not ascending :
516- _sorted = _sorted [::- 1 ]
508+ codes = codes [::- 1 ]
509+
517510 if inplace :
518- self ._codes = _sorted
511+ self ._codes = codes
519512 return
520513 else :
521- return Categorical (values = _sorted ,levels = self .levels , ordered = self .ordered ,
514+ return Categorical (values = codes ,levels = self .levels , ordered = self .ordered ,
522515 name = self .name , fastpath = True )
523516
524517
525- def sort (self , inplace = True , ascending = True , ** kwargs ):
518+ def sort (self , inplace = True , ascending = True , na_position = 'last' , ** kwargs ):
526519 """ Sorts the Category inplace by level value.
527520
528521 Only ordered Categoricals can be sorted!
@@ -533,11 +526,11 @@ def sort(self, inplace=True, ascending=True, **kwargs):
533526 ----------
534527 ascending : boolean, default True
535528 Sort ascending. Passing False sorts descending
529+ inplace : boolean, default False
530+ Do operation in place.
536531 na_position : {'first', 'last'} (optional, default='last')
537532 'first' puts NaNs at the beginning
538533 'last' puts NaNs at the end
539- inplace : boolean, default False
540- Do operation in place.
541534
542535 Returns
543536 -------
@@ -932,3 +925,20 @@ def describe(self):
932925 result .index .name = 'levels'
933926 result .columns = ['counts' ,'freqs' ]
934927 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