@@ -811,10 +811,10 @@ 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 ):
814
+ def test_map_with_dict_and_series (self ):
815
815
expected = Index (['foo' , 'bar' , 'baz' ])
816
816
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 )
818
818
819
819
# GH 12766
820
820
# special = []
@@ -824,41 +824,58 @@ def test_map_with_series_all_indices(self):
824
824
orig_values = ['a' , 'B' , 1 , 'a' ]
825
825
new_values = ['one' , 2 , 3.0 , 'one' ]
826
826
cur_index = CategoricalIndex (orig_values , name = 'XXX' )
827
+ expected = CategoricalIndex (new_values ,
828
+ name = 'XXX' , categories = [3.0 , 2 , 'one' ])
829
+
827
830
mapper = pd .Series (new_values [:- 1 ], index = orig_values [:- 1 ])
828
- expected = CategoricalIndex (new_values , name = 'XXX' )
829
831
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 )
832
840
833
841
for name in list (set (self .indices .keys ()) - set (special )):
834
842
cur_index = self .indices [name ]
835
843
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 ))
840
856
841
857
def test_map_with_categorical_series (self ):
842
858
# GH 12756
843
859
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" )
845
862
c = Series (["even" , "odd" , "even" , "odd" ])
846
863
847
864
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 )
849
866
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 )
851
868
852
869
def test_map_with_non_function_missing_values (self ):
853
870
# GH 12756
854
871
expected = Index ([2. , np .nan , 'foo' ])
855
872
input = Index ([2 , 1 , 0 ])
856
873
857
874
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 ))
859
876
860
877
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 ))
862
879
863
880
def test_append_multiple (self ):
864
881
index = Index (['a' , 'b' , 'c' , 'd' , 'e' , 'f' ])
0 commit comments