@@ -963,17 +963,13 @@ def __getitem__(self, key):
963
963
else :
964
964
return result
965
965
966
- def append (self , other ):
966
+ def _ensure_compat_append (self , other ):
967
967
"""
968
- Append a collection of Index options together
969
-
970
- Parameters
971
- ----------
972
- other : Index or list/tuple of indices
968
+ prepare the append
973
969
974
970
Returns
975
971
-------
976
- appended : Index
972
+ list of to_concat, name of result Index
977
973
"""
978
974
name = self .name
979
975
to_concat = [self ]
@@ -991,7 +987,21 @@ def append(self, other):
991
987
to_concat = self ._ensure_compat_concat (to_concat )
992
988
to_concat = [x .values if isinstance (x , Index ) else x
993
989
for x in to_concat ]
990
+ return to_concat , name
991
+
992
+ def append (self , other ):
993
+ """
994
+ Append a collection of Index options together
994
995
996
+ Parameters
997
+ ----------
998
+ other : Index or list/tuple of indices
999
+
1000
+ Returns
1001
+ -------
1002
+ appended : Index
1003
+ """
1004
+ to_concat , name = self ._ensure_compat_append (other )
995
1005
return Index (np .concatenate (to_concat ), name = name )
996
1006
997
1007
@staticmethod
@@ -2628,6 +2638,10 @@ def codes(self):
2628
2638
def categories (self ):
2629
2639
return self ._data .categories
2630
2640
2641
+ @property
2642
+ def ordered (self ):
2643
+ return self ._data .ordered
2644
+
2631
2645
def __contains__ (self , key ):
2632
2646
hash (key )
2633
2647
return key in self .categories
@@ -2760,11 +2774,48 @@ def insert(self, loc, item):
2760
2774
2761
2775
from pandas import Categorical
2762
2776
codes = self .codes
2763
- idx = np .concatenate (
2777
+ codes = np .concatenate (
2764
2778
(codes [:loc ], code , codes [loc :]))
2765
- cat = Categorical .from_codes (idx , categories = self .categories )
2779
+ cat = Categorical .from_codes (codes , categories = self .categories )
2766
2780
return CategoricalIndex (cat , name = self .name )
2767
2781
2782
+ def append (self , other ):
2783
+ """
2784
+ Append a collection of CategoricalIndex options together
2785
+
2786
+ Parameters
2787
+ ----------
2788
+ other : Index or list/tuple of indices
2789
+
2790
+ Returns
2791
+ -------
2792
+ appended : Index
2793
+
2794
+ Raises
2795
+ ------
2796
+ ValueError if other is not in the categories
2797
+ """
2798
+ from pandas import Categorical
2799
+ categories = self .categories
2800
+ to_concat , name = self ._ensure_compat_append (other )
2801
+
2802
+ def convert (c ):
2803
+ """ make sure that we have the same categories exactly """
2804
+ if is_categorical_dtype (c ):
2805
+ if not c .categories .equals (categories ):
2806
+ raise ValueError ("categories must match existing categories when appending" )
2807
+ else :
2808
+ c = Categorical (c , categories )
2809
+ if isnull (c ).any ():
2810
+ raise ValueError ("cannot append a non-category item to a CategoricalIndex" )
2811
+
2812
+ return c
2813
+
2814
+ to_concat = [ convert (c ) for c in to_concat ]
2815
+
2816
+ codes = np .concatenate ([ c .codes for c in to_concat ])
2817
+ cat = Categorical .from_codes (codes , categories = categories )
2818
+ return CategoricalIndex (cat , name = name )
2768
2819
2769
2820
CategoricalIndex ._add_numeric_methods_disabled ()
2770
2821
CategoricalIndex ._add_logical_methods_disabled ()
0 commit comments