@@ -823,60 +823,32 @@ def test_map_tseries_indices_return_index(self):
823
823
tm .assert_index_equal (exp , date_index .map (lambda x : x .hour ))
824
824
825
825
def test_map_with_dict_and_series (self ):
826
+ # GH 12756
826
827
expected = Index (['foo' , 'bar' , 'baz' ])
827
828
mapper = Series (expected .values , index = [0 , 1 , 2 ])
828
- tm .assert_index_equal (tm .makeIntIndex (3 ).map (mapper ), expected )
829
-
830
- # GH 12766
831
- # special = []
832
- special = ['catIndex' ]
833
-
834
- for name in special :
835
- orig_values = ['a' , 'B' , 1 , 'a' ]
836
- new_values = ['one' , 2 , 3.0 , 'one' ]
837
- cur_index = CategoricalIndex (orig_values , name = 'XXX' )
838
- expected = CategoricalIndex (new_values ,
839
- name = 'XXX' , categories = [3.0 , 2 , 'one' ])
840
-
841
- mapper = pd .Series (new_values [:- 1 ], index = orig_values [:- 1 ])
842
- output = cur_index .map (mapper )
843
- # Order of categories in output can be different
844
- tm .assert_index_equal (expected , output )
829
+ result = tm .makeIntIndex (3 ).map (mapper )
830
+ tm .assert_index_equal (result , expected )
845
831
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 )
832
+ for name in self .indices .keys ():
833
+ if name == 'catIndex' :
834
+ # Tested in test_categorical
835
+ continue
851
836
852
- for name in list (set (self .indices .keys ()) - set (special )):
853
837
cur_index = self .indices [name ]
854
838
expected = Index (np .arange (len (cur_index ), 0 , - 1 ))
855
839
mapper = pd .Series (expected , index = cur_index )
856
840
tm .assert_index_equal (expected , cur_index .map (mapper ))
857
841
858
842
mapper = {o : n for o , n in
859
843
zip (cur_index , expected )}
844
+ # If the mapper is empty the expected index type is Int64Index
845
+ # but the output defaults to Float64 so I treat it independently
860
846
if mapper :
861
847
tm .assert_index_equal (expected , cur_index .map (mapper ))
862
848
else :
863
- # The expected index type is Int64Index
864
- # but the output defaults to Float64
865
849
tm .assert_index_equal (Float64Index ([]),
866
850
cur_index .map (mapper ))
867
851
868
- def test_map_with_categorical_series (self ):
869
- # GH 12756
870
- a = Index ([1 , 2 , 3 , 4 ])
871
- b = Series (["even" , "odd" , "even" , "odd" ],
872
- dtype = "category" )
873
- c = Series (["even" , "odd" , "even" , "odd" ])
874
-
875
- exp = CategoricalIndex (["odd" , "even" , "odd" , np .nan ])
876
- tm .assert_index_equal (a .map (b ), exp )
877
- exp = Index (["odd" , "even" , "odd" , np .nan ])
878
- tm .assert_index_equal (a .map (c ), exp )
879
-
880
852
def test_map_with_non_function_missing_values (self ):
881
853
# GH 12756
882
854
expected = Index ([2. , np .nan , 'foo' ])
0 commit comments