@@ -822,10 +822,10 @@ def test_map_tseries_indices_return_index(self):
822
822
exp = Index (range (24 ), name = 'hourly' )
823
823
tm .assert_index_equal (exp , date_index .map (lambda x : x .hour ))
824
824
825
- def test_map_with_series_all_indices (self ):
825
+ def test_map_with_dict_and_series (self ):
826
826
expected = Index (['foo' , 'bar' , 'baz' ])
827
827
mapper = Series (expected .values , index = [0 , 1 , 2 ])
828
- self .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
828
+ tm .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
829
829
830
830
# GH 12766
831
831
# special = []
@@ -835,41 +835,58 @@ def test_map_with_series_all_indices(self):
835
835
orig_values = ['a' , 'B' , 1 , 'a' ]
836
836
new_values = ['one' , 2 , 3.0 , 'one' ]
837
837
cur_index = CategoricalIndex (orig_values , name = 'XXX' )
838
+ expected = CategoricalIndex (new_values ,
839
+ name = 'XXX' , categories = [3.0 , 2 , 'one' ])
840
+
838
841
mapper = pd .Series (new_values [:- 1 ], index = orig_values [:- 1 ])
839
- expected = CategoricalIndex (new_values , name = 'XXX' )
840
842
output = cur_index .map (mapper )
841
- self .assert_numpy_array_equal (expected .values .get_values (), output .values .get_values ())
842
- self .assert_equal (expected .name , output .name )
843
+ # Order of categories in output can be different
844
+ tm .assert_index_equal (expected , output )
845
+
846
+ mapper = {o : n for o , n in
847
+ zip (orig_values [:- 1 ], new_values [:- 1 ])}
848
+ output = cur_index .map (mapper )
849
+ # Order of categories in output can be different
850
+ tm .assert_index_equal (expected , output )
843
851
844
852
for name in list (set (self .indices .keys ()) - set (special )):
845
853
cur_index = self .indices [name ]
846
854
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 ))
855
+ mapper = pd .Series (expected , index = cur_index )
856
+ tm .assert_index_equal (expected , cur_index .map (mapper ))
857
+
858
+ mapper = {o : n for o , n in
859
+ zip (cur_index , expected )}
860
+ if mapper :
861
+ tm .assert_index_equal (expected , cur_index .map (mapper ))
862
+ else :
863
+ # The expected index type is Int64Index
864
+ # but the output defaults to Float64
865
+ tm .assert_index_equal (Float64Index ([]),
866
+ cur_index .map (mapper ))
851
867
852
868
def test_map_with_categorical_series (self ):
853
869
# GH 12756
854
870
a = Index ([1 , 2 , 3 , 4 ])
855
- b = Series (["even" , "odd" , "even" , "odd" ], dtype = "category" )
871
+ b = Series (["even" , "odd" , "even" , "odd" ],
872
+ dtype = "category" )
856
873
c = Series (["even" , "odd" , "even" , "odd" ])
857
874
858
875
exp = CategoricalIndex (["odd" , "even" , "odd" , np .nan ])
859
- self .assert_index_equal (a .map (b ), exp )
876
+ tm .assert_index_equal (a .map (b ), exp )
860
877
exp = Index (["odd" , "even" , "odd" , np .nan ])
861
- self .assert_index_equal (a .map (c ), exp )
878
+ tm .assert_index_equal (a .map (c ), exp )
862
879
863
880
def test_map_with_non_function_missing_values (self ):
864
881
# GH 12756
865
882
expected = Index ([2. , np .nan , 'foo' ])
866
883
input = Index ([2 , 1 , 0 ])
867
884
868
885
mapper = Series (['foo' , 2. , 'baz' ], index = [0 , 2 , - 1 ])
869
- self .assert_index_equal (expected , input .map (mapper ))
886
+ tm .assert_index_equal (expected , input .map (mapper ))
870
887
871
888
mapper = {0 : 'foo' , 2 : 2.0 , - 1 : 'baz' }
872
- self .assert_index_equal (expected , input .map (mapper ))
889
+ tm .assert_index_equal (expected , input .map (mapper ))
873
890
874
891
def test_append_multiple (self ):
875
892
index = Index (['a' , 'b' , 'c' , 'd' , 'e' , 'f' ])
0 commit comments