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