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