@@ -15,124 +15,123 @@ def na_action(request):
15
15
return request .param
16
16
17
17
18
- class TestMap :
19
- @pytest .mark .parametrize (
20
- "data, categories" ,
21
- [
22
- (list ("abcbca" ), list ("cab" )),
23
- (pd .interval_range (0 , 3 ).repeat (3 ), pd .interval_range (0 , 3 )),
24
- ],
25
- ids = ["string" , "interval" ],
18
+ @pytest .mark .parametrize (
19
+ "data, categories" ,
20
+ [
21
+ (list ("abcbca" ), list ("cab" )),
22
+ (pd .interval_range (0 , 3 ).repeat (3 ), pd .interval_range (0 , 3 )),
23
+ ],
24
+ ids = ["string" , "interval" ],
25
+ )
26
+ def test_map_str (data , categories , ordered , na_action ):
27
+ # GH 31202 - override base class since we want to maintain categorical/ordered
28
+ cat = Categorical (data , categories = categories , ordered = ordered )
29
+ result = cat .map (str , na_action = na_action )
30
+ expected = Categorical (
31
+ map (str , data ), categories = map (str , categories ), ordered = ordered
26
32
)
27
- def test_map_str (self , data , categories , ordered , na_action ):
28
- # GH 31202 - override base class since we want to maintain categorical/ordered
29
- cat = Categorical (data , categories = categories , ordered = ordered )
30
- result = cat .map (str , na_action = na_action )
31
- expected = Categorical (
32
- map (str , data ), categories = map (str , categories ), ordered = ordered
33
- )
33
+ tm .assert_categorical_equal (result , expected )
34
+
35
+ def test_map (na_action ):
36
+ cat = Categorical (list ("ABABC" ), categories = list ("CBA" ), ordered = True )
37
+ result = cat .map (lambda x : x .lower (), na_action = na_action )
38
+ exp = Categorical (list ("ababc" ), categories = list ("cba" ), ordered = True )
39
+ tm .assert_categorical_equal (result , exp )
40
+
41
+ cat = Categorical (list ("ABABC" ), categories = list ("BAC" ), ordered = False )
42
+ result = cat .map (lambda x : x .lower (), na_action = na_action )
43
+ exp = Categorical (list ("ababc" ), categories = list ("bac" ), ordered = False )
44
+ tm .assert_categorical_equal (result , exp )
45
+
46
+ # GH 12766: Return an index not an array
47
+ result = cat .map (lambda x : 1 , na_action = na_action )
48
+ exp = Index (np .array ([1 ] * 5 , dtype = np .int64 ))
49
+ tm .assert_index_equal (result , exp )
50
+
51
+ # change categories dtype
52
+ cat = Categorical (list ("ABABC" ), categories = list ("BAC" ), ordered = False )
53
+
54
+ def f (x ):
55
+ return {"A" : 10 , "B" : 20 , "C" : 30 }.get (x )
56
+
57
+ result = cat .map (f , na_action = na_action )
58
+ exp = Categorical ([10 , 20 , 10 , 20 , 30 ], categories = [20 , 10 , 30 ], ordered = False )
59
+ tm .assert_categorical_equal (result , exp )
60
+
61
+ mapper = Series ([10 , 20 , 30 ], index = ["A" , "B" , "C" ])
62
+ result = cat .map (mapper , na_action = na_action )
63
+ tm .assert_categorical_equal (result , exp )
64
+
65
+ result = cat .map ({"A" : 10 , "B" : 20 , "C" : 30 }, na_action = na_action )
66
+ tm .assert_categorical_equal (result , exp )
67
+
68
+ @pytest .mark .parametrize (
69
+ ("data" , "f" , "expected" ),
70
+ (
71
+ ([1 , 1 , np .nan ], pd .isna , Index ([False , False , True ])),
72
+ ([1 , 2 , np .nan ], pd .isna , Index ([False , False , True ])),
73
+ ([1 , 1 , np .nan ], {1 : False }, Categorical ([False , False , np .nan ])),
74
+ ([1 , 2 , np .nan ], {1 : False , 2 : False }, Index ([False , False , np .nan ])),
75
+ (
76
+ [1 , 1 , np .nan ],
77
+ Series ([False , False ]),
78
+ Categorical ([False , False , np .nan ]),
79
+ ),
80
+ (
81
+ [1 , 2 , np .nan ],
82
+ Series ([False ] * 3 ),
83
+ Index ([False , False , np .nan ]),
84
+ ),
85
+ ),
86
+ )
87
+ def test_map_with_nan_none (data , f , expected ): # GH 24241
88
+ values = Categorical (data )
89
+ result = values .map (f , na_action = None )
90
+ if isinstance (expected , Categorical ):
34
91
tm .assert_categorical_equal (result , expected )
35
-
36
- def test_map (self , na_action ):
37
- cat = Categorical (list ("ABABC" ), categories = list ("CBA" ), ordered = True )
38
- result = cat .map (lambda x : x .lower (), na_action = na_action )
39
- exp = Categorical (list ("ababc" ), categories = list ("cba" ), ordered = True )
40
- tm .assert_categorical_equal (result , exp )
41
-
42
- cat = Categorical (list ("ABABC" ), categories = list ("BAC" ), ordered = False )
43
- result = cat .map (lambda x : x .lower (), na_action = na_action )
44
- exp = Categorical (list ("ababc" ), categories = list ("bac" ), ordered = False )
45
- tm .assert_categorical_equal (result , exp )
46
-
47
- # GH 12766: Return an index not an array
48
- result = cat .map (lambda x : 1 , na_action = na_action )
49
- exp = Index (np .array ([1 ] * 5 , dtype = np .int64 ))
50
- tm .assert_index_equal (result , exp )
51
-
52
- # change categories dtype
53
- cat = Categorical (list ("ABABC" ), categories = list ("BAC" ), ordered = False )
54
-
55
- def f (x ):
56
- return {"A" : 10 , "B" : 20 , "C" : 30 }.get (x )
57
-
58
- result = cat .map (f , na_action = na_action )
59
- exp = Categorical ([10 , 20 , 10 , 20 , 30 ], categories = [20 , 10 , 30 ], ordered = False )
60
- tm .assert_categorical_equal (result , exp )
61
-
62
- mapper = Series ([10 , 20 , 30 ], index = ["A" , "B" , "C" ])
63
- result = cat .map (mapper , na_action = na_action )
64
- tm .assert_categorical_equal (result , exp )
65
-
66
- result = cat .map ({"A" : 10 , "B" : 20 , "C" : 30 }, na_action = na_action )
67
- tm .assert_categorical_equal (result , exp )
68
-
69
- @pytest .mark .parametrize (
70
- ("data" , "f" , "expected" ),
92
+ else :
93
+ tm .assert_index_equal (result , expected )
94
+
95
+ @pytest .mark .parametrize (
96
+ ("data" , "f" , "expected" ),
97
+ (
98
+ ([1 , 1 , np .nan ], pd .isna , Categorical ([False , False , np .nan ])),
99
+ ([1 , 2 , np .nan ], pd .isna , Index ([False , False , np .nan ])),
100
+ ([1 , 1 , np .nan ], {1 : False }, Categorical ([False , False , np .nan ])),
101
+ ([1 , 2 , np .nan ], {1 : False , 2 : False }, Index ([False , False , np .nan ])),
71
102
(
72
- ([1 , 1 , np .nan ], pd .isna , Index ([False , False , True ])),
73
- ([1 , 2 , np .nan ], pd .isna , Index ([False , False , True ])),
74
- ([1 , 1 , np .nan ], {1 : False }, Categorical ([False , False , np .nan ])),
75
- ([1 , 2 , np .nan ], {1 : False , 2 : False }, Index ([False , False , np .nan ])),
76
- (
77
- [1 , 1 , np .nan ],
78
- Series ([False , False ]),
79
- Categorical ([False , False , np .nan ]),
80
- ),
81
- (
82
- [1 , 2 , np .nan ],
83
- Series ([False ] * 3 ),
84
- Index ([False , False , np .nan ]),
85
- ),
103
+ [1 , 1 , np .nan ],
104
+ Series ([False , False ]),
105
+ Categorical ([False , False , np .nan ]),
86
106
),
87
- )
88
- def test_map_with_nan_none (self , data , f , expected ): # GH 24241
89
- values = Categorical (data )
90
- result = values .map (f , na_action = None )
91
- if isinstance (expected , Categorical ):
92
- tm .assert_categorical_equal (result , expected )
93
- else :
94
- tm .assert_index_equal (result , expected )
95
-
96
- @pytest .mark .parametrize (
97
- ("data" , "f" , "expected" ),
98
107
(
99
- ([1 , 1 , np .nan ], pd .isna , Categorical ([False , False , np .nan ])),
100
- ([1 , 2 , np .nan ], pd .isna , Index ([False , False , np .nan ])),
101
- ([1 , 1 , np .nan ], {1 : False }, Categorical ([False , False , np .nan ])),
102
- ([1 , 2 , np .nan ], {1 : False , 2 : False }, Index ([False , False , np .nan ])),
103
- (
104
- [1 , 1 , np .nan ],
105
- Series ([False , False ]),
106
- Categorical ([False , False , np .nan ]),
107
- ),
108
- (
109
- [1 , 2 , np .nan ],
110
- Series ([False , False , False ]),
111
- Index ([False , False , np .nan ]),
112
- ),
108
+ [1 , 2 , np .nan ],
109
+ Series ([False , False , False ]),
110
+ Index ([False , False , np .nan ]),
113
111
),
114
- )
115
- def test_map_with_nan_ignore (self , data , f , expected ): # GH 24241
116
- values = Categorical (data )
117
- result = values .map (f , na_action = "ignore" )
118
- if data [1 ] == 1 :
119
- tm .assert_categorical_equal (result , expected )
120
- else :
121
- tm .assert_index_equal (result , expected )
122
-
123
- def test_map_with_dict_or_series (self , na_action ):
124
- orig_values = ["a" , "B" , 1 , "a" ]
125
- new_values = ["one" , 2 , 3.0 , "one" ]
126
- cat = Categorical (orig_values )
127
-
128
- mapper = Series (new_values [:- 1 ], index = orig_values [:- 1 ])
129
- result = cat .map (mapper , na_action = na_action )
130
-
131
- # Order of categories in result can be different
132
- expected = Categorical (new_values , categories = [3.0 , 2 , "one" ])
112
+ ),
113
+ )
114
+ def test_map_with_nan_ignore (data , f , expected ): # GH 24241
115
+ values = Categorical (data )
116
+ result = values .map (f , na_action = "ignore" )
117
+ if data [1 ] == 1 :
133
118
tm .assert_categorical_equal (result , expected )
119
+ else :
120
+ tm .assert_index_equal (result , expected )
134
121
135
- mapper = dict (zip (orig_values [:- 1 ], new_values [:- 1 ]))
136
- result = cat .map (mapper , na_action = na_action )
137
- # Order of categories in result can be different
138
- tm .assert_categorical_equal (result , expected )
122
+ def test_map_with_dict_or_series (na_action ):
123
+ orig_values = ["a" , "B" , 1 , "a" ]
124
+ new_values = ["one" , 2 , 3.0 , "one" ]
125
+ cat = Categorical (orig_values )
126
+
127
+ mapper = Series (new_values [:- 1 ], index = orig_values [:- 1 ])
128
+ result = cat .map (mapper , na_action = na_action )
129
+
130
+ # Order of categories in result can be different
131
+ expected = Categorical (new_values , categories = [3.0 , 2 , "one" ])
132
+ tm .assert_categorical_equal (result , expected )
133
+
134
+ mapper = dict (zip (orig_values [:- 1 ], new_values [:- 1 ]))
135
+ result = cat .map (mapper , na_action = na_action )
136
+ # Order of categories in result can be different
137
+ tm .assert_categorical_equal (result , expected )
0 commit comments