@@ -809,6 +809,52 @@ def test_map_tseries_indices_return_index(self):
809
809
exp = Index (range (24 ), name = 'hourly' )
810
810
tm .assert_index_equal (exp , date_index .map (lambda x : x .hour ))
811
811
812
+ def test_map_with_series_all_indices (self ):
813
+ expected = Index (['foo' , 'bar' , 'baz' ])
814
+ mapper = Series (expected .values , index = [0 , 1 , 2 ])
815
+ self .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
816
+
817
+ # GH 12766
818
+ # special = []
819
+ special = ['catIndex' ]
820
+
821
+ for name in special :
822
+ orig_values = ['a' , 'B' , 1 , 'a' ]
823
+ new_values = ['one' , 2 , 3.0 , 'one' ]
824
+ cur_index = CategoricalIndex (orig_values , name = 'XXX' )
825
+ mapper = pd .Series (new_values [:- 1 ], index = orig_values [:- 1 ])
826
+ expected = CategoricalIndex (new_values , name = 'XXX' )
827
+ output = cur_index .map (mapper )
828
+ self .assert_numpy_array_equal (expected .values .get_values (), output .values .get_values ())
829
+ self .assert_equal (expected .name , output .name )
830
+
831
+
832
+ for name in list (set (self .indices .keys ()) - set (special )):
833
+ cur_index = self .indices [name ]
834
+ expected = Index (np .arange (len (cur_index ), 0 , - 1 ))
835
+ mapper = pd .Series (expected .values , index = cur_index )
836
+ print (name )
837
+ output = cur_index .map (mapper )
838
+ self .assert_index_equal (expected , cur_index .map (mapper ))
839
+
840
+ def test_map_with_categorical_series (self ):
841
+ # GH 12756
842
+ a = Index ([1 , 2 , 3 , 4 ])
843
+ b = Series (["even" , "odd" , "even" , "odd" ], dtype = "category" )
844
+ c = Series (["even" , "odd" , "even" , "odd" ])
845
+
846
+ exp = CategoricalIndex (["odd" , "even" , "odd" , np .nan ])
847
+ self .assert_index_equal (a .map (b ), exp )
848
+ exp = Index (["odd" , "even" , "odd" , np .nan ])
849
+ self .assert_index_equal (a .map (c ), exp )
850
+
851
+ def test_map_with_series_missing_values (self ):
852
+ # GH 12756
853
+ expected = Index ([2. , np .nan , 'foo' ])
854
+ mapper = Series (['foo' , 2. , 'baz' ], index = [0 , 2 , - 1 ])
855
+ output = Index ([2 , 1 , 0 ]).map (mapper )
856
+ self .assert_index_equal (output , expected )
857
+
812
858
def test_append_multiple (self ):
813
859
index = Index (['a' , 'b' , 'c' , 'd' , 'e' , 'f' ])
814
860
0 commit comments