@@ -811,10 +811,10 @@ def test_map_tseries_indices_return_index(self):
811811 exp = Index (range (24 ), name = 'hourly' )
812812 tm .assert_index_equal (exp , date_index .map (lambda x : x .hour ))
813813
814- def test_map_with_series_all_indices (self ):
814+ def test_map_with_dict_and_series (self ):
815815 expected = Index (['foo' , 'bar' , 'baz' ])
816816 mapper = Series (expected .values , index = [0 , 1 , 2 ])
817- self .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
817+ tm .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
818818
819819 # GH 12766
820820 # special = []
@@ -824,41 +824,58 @@ def test_map_with_series_all_indices(self):
824824 orig_values = ['a' , 'B' , 1 , 'a' ]
825825 new_values = ['one' , 2 , 3.0 , 'one' ]
826826 cur_index = CategoricalIndex (orig_values , name = 'XXX' )
827+ expected = CategoricalIndex (new_values ,
828+ name = 'XXX' , categories = [3.0 , 2 , 'one' ])
829+
827830 mapper = pd .Series (new_values [:- 1 ], index = orig_values [:- 1 ])
828- expected = CategoricalIndex (new_values , name = 'XXX' )
829831 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+ # Order of categories in output can be different
833+ tm .assert_index_equal (expected , output )
834+
835+ mapper = {o : n for o , n in
836+ zip (orig_values [:- 1 ], new_values [:- 1 ])}
837+ output = cur_index .map (mapper )
838+ # Order of categories in output can be different
839+ tm .assert_index_equal (expected , output )
832840
833841 for name in list (set (self .indices .keys ()) - set (special )):
834842 cur_index = self .indices [name ]
835843 expected = Index (np .arange (len (cur_index ), 0 , - 1 ))
836- mapper = pd .Series (expected .values , index = cur_index )
837- print (name )
838- output = cur_index .map (mapper )
839- self .assert_index_equal (expected , cur_index .map (mapper ))
844+ mapper = pd .Series (expected , index = cur_index )
845+ tm .assert_index_equal (expected , cur_index .map (mapper ))
846+
847+ mapper = {o : n for o , n in
848+ zip (cur_index , expected )}
849+ if mapper :
850+ tm .assert_index_equal (expected , cur_index .map (mapper ))
851+ else :
852+ # The expected index type is Int64Index
853+ # but the output defaults to Float64
854+ tm .assert_index_equal (Float64Index ([]),
855+ cur_index .map (mapper ))
840856
841857 def test_map_with_categorical_series (self ):
842858 # GH 12756
843859 a = Index ([1 , 2 , 3 , 4 ])
844- b = Series (["even" , "odd" , "even" , "odd" ], dtype = "category" )
860+ b = Series (["even" , "odd" , "even" , "odd" ],
861+ dtype = "category" )
845862 c = Series (["even" , "odd" , "even" , "odd" ])
846863
847864 exp = CategoricalIndex (["odd" , "even" , "odd" , np .nan ])
848- self .assert_index_equal (a .map (b ), exp )
865+ tm .assert_index_equal (a .map (b ), exp )
849866 exp = Index (["odd" , "even" , "odd" , np .nan ])
850- self .assert_index_equal (a .map (c ), exp )
867+ tm .assert_index_equal (a .map (c ), exp )
851868
852869 def test_map_with_non_function_missing_values (self ):
853870 # GH 12756
854871 expected = Index ([2. , np .nan , 'foo' ])
855872 input = Index ([2 , 1 , 0 ])
856873
857874 mapper = Series (['foo' , 2. , 'baz' ], index = [0 , 2 , - 1 ])
858- self .assert_index_equal (expected , input .map (mapper ))
875+ tm .assert_index_equal (expected , input .map (mapper ))
859876
860877 mapper = {0 : 'foo' , 2 : 2.0 , - 1 : 'baz' }
861- self .assert_index_equal (expected , input .map (mapper ))
878+ tm .assert_index_equal (expected , input .map (mapper ))
862879
863880 def test_append_multiple (self ):
864881 index = Index (['a' , 'b' , 'c' , 'd' , 'e' , 'f' ])
0 commit comments