13
13
import pandas ._testing as tm
14
14
15
15
16
- def test_applymap (float_frame ):
17
- result = float_frame .applymap (lambda x : x * 2 )
16
+ def test_map (float_frame ):
17
+ result = float_frame .map (lambda x : x * 2 )
18
18
tm .assert_frame_equal (result , float_frame * 2 )
19
- float_frame .applymap (type )
19
+ float_frame .map (type )
20
20
21
21
# GH 465: function returning tuples
22
- result = float_frame .applymap (lambda x : (x , x ))["A" ][0 ]
22
+ result = float_frame .map (lambda x : (x , x ))["A" ][0 ]
23
23
assert isinstance (result , tuple )
24
24
25
25
26
26
@pytest .mark .parametrize ("val" , [1 , 1.0 ])
27
27
def test_applymap_float_object_conversion (val ):
28
28
# GH 2909: object conversion to float in constructor?
29
29
df = DataFrame (data = [val , "a" ])
30
- result = df .applymap (lambda x : x ).dtypes [0 ]
30
+ result = df .map (lambda x : x ).dtypes [0 ]
31
31
assert result == object
32
32
33
33
@@ -41,15 +41,15 @@ def test_applymap_keeps_dtype(na_action):
41
41
def func (x ):
42
42
return str .upper (x ) if not pd .isna (x ) else x
43
43
44
- result = df .applymap (func , na_action = na_action )
44
+ result = df .map (func , na_action = na_action )
45
45
46
46
expected_sparse = pd .array (["A" , np .nan , "B" ], dtype = pd .SparseDtype (object ))
47
47
expected_arr = expected_sparse .astype (object )
48
48
expected = DataFrame ({"a" : expected_arr , "b" : expected_sparse })
49
49
50
50
tm .assert_frame_equal (result , expected )
51
51
52
- result_empty = df .iloc [:0 , :].applymap (func , na_action = na_action )
52
+ result_empty = df .iloc [:0 , :].map (func , na_action = na_action )
53
53
expected_empty = expected .iloc [:0 , :]
54
54
tm .assert_frame_equal (result_empty , expected_empty )
55
55
@@ -61,9 +61,9 @@ def test_applymap_str():
61
61
cols = ["a" , "a" , "a" , "a" ]
62
62
df .columns = cols
63
63
64
- expected = df2 .applymap (str )
64
+ expected = df2 .map (str )
65
65
expected .columns = cols
66
- result = df .applymap (str )
66
+ result = df .map (str )
67
67
tm .assert_frame_equal (result , expected )
68
68
69
69
@@ -75,7 +75,7 @@ def test_applymap_datetimelike(col, val):
75
75
# datetime/timedelta
76
76
df = DataFrame (np .random .random ((3 , 4 )))
77
77
df [col ] = val
78
- result = df .applymap (str )
78
+ result = df .map (str )
79
79
assert result .loc [0 , col ] == str (df .loc [0 , col ])
80
80
81
81
@@ -91,24 +91,24 @@ def test_applymap_datetimelike(col, val):
91
91
@pytest .mark .parametrize ("func" , [round , lambda x : x ])
92
92
def test_applymap_empty (expected , func ):
93
93
# GH 8222
94
- result = expected .applymap (func )
94
+ result = expected .map (func )
95
95
tm .assert_frame_equal (result , expected )
96
96
97
97
98
98
def test_applymap_kwargs ():
99
99
# GH 40652
100
- result = DataFrame ([[1 , 2 ], [3 , 4 ]]).applymap (lambda x , y : x + y , y = 2 )
100
+ result = DataFrame ([[1 , 2 ], [3 , 4 ]]).map (lambda x , y : x + y , y = 2 )
101
101
expected = DataFrame ([[3 , 4 ], [5 , 6 ]])
102
102
tm .assert_frame_equal (result , expected )
103
103
104
104
105
105
def test_applymap_na_ignore (float_frame ):
106
106
# GH 23803
107
- strlen_frame = float_frame .applymap (lambda x : len (str (x )))
107
+ strlen_frame = float_frame .map (lambda x : len (str (x )))
108
108
float_frame_with_na = float_frame .copy ()
109
109
mask = np .random .randint (0 , 2 , size = float_frame .shape , dtype = bool )
110
110
float_frame_with_na [mask ] = pd .NA
111
- strlen_frame_na_ignore = float_frame_with_na .applymap (
111
+ strlen_frame_na_ignore = float_frame_with_na .map (
112
112
lambda x : len (str (x )), na_action = "ignore"
113
113
)
114
114
strlen_frame_with_na = strlen_frame .copy ()
@@ -124,7 +124,7 @@ def func(x):
124
124
return (x .hour , x .day , x .month )
125
125
126
126
# it works!
127
- DataFrame (ser ).applymap (func )
127
+ DataFrame (ser ).map (func )
128
128
129
129
130
130
def test_applymap_box ():
@@ -144,7 +144,7 @@ def test_applymap_box():
144
144
}
145
145
)
146
146
147
- result = df .applymap (lambda x : type (x ).__name__ )
147
+ result = df .map (lambda x : type (x ).__name__ )
148
148
expected = DataFrame (
149
149
{
150
150
"a" : ["Timestamp" , "Timestamp" ],
@@ -161,8 +161,8 @@ def test_frame_applymap_dont_convert_datetime64():
161
161
162
162
df = DataFrame ({"x1" : [datetime (1996 , 1 , 1 )]})
163
163
164
- df = df .applymap (lambda x : x + BDay ())
165
- df = df .applymap (lambda x : x + BDay ())
164
+ df = df .map (lambda x : x + BDay ())
165
+ df = df .map (lambda x : x + BDay ())
166
166
167
167
result = df .x1 .dtype
168
168
assert result == "M8[ns]"
@@ -182,7 +182,7 @@ def non_reducing_function(val):
182
182
for func in [reducing_function , non_reducing_function ]:
183
183
del values [:]
184
184
185
- df .applymap (func )
185
+ df .map (func )
186
186
assert values == df .a .to_list ()
187
187
188
188
@@ -193,15 +193,23 @@ def test_applymap_type():
193
193
index = ["a" , "b" , "c" ],
194
194
)
195
195
196
- result = df .applymap (type )
196
+ result = df .map (type )
197
197
expected = DataFrame (
198
198
{"col1" : [int , str , type ], "col2" : [float , datetime , float ]},
199
199
index = ["a" , "b" , "c" ],
200
200
)
201
201
tm .assert_frame_equal (result , expected )
202
202
203
203
204
- def test_applymap_invalid_na_action (float_frame ):
204
+ def test_map_invalid_na_action (float_frame ):
205
205
# GH 23803
206
206
with pytest .raises (ValueError , match = "na_action must be .*Got 'abc'" ):
207
- float_frame .applymap (lambda x : len (str (x )), na_action = "abc" )
207
+ float_frame .map (lambda x : len (str (x )), na_action = "abc" )
208
+
209
+
210
+ def test_applymap_deprecated ():
211
+ # GH52353
212
+ df = DataFrame ({"a" : [1 , 2 , 3 ]})
213
+ msg = "DataFrame.applymap has been deprecated. Use DataFrame.map instead."
214
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
215
+ df .applymap (lambda x : x )
0 commit comments