1313import pandas ._testing as tm
1414
1515
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 )
1818 tm .assert_frame_equal (result , float_frame * 2 )
19- float_frame .applymap (type )
19+ float_frame .map (type )
2020
2121 # 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 ]
2323 assert isinstance (result , tuple )
2424
2525
2626@pytest .mark .parametrize ("val" , [1 , 1.0 ])
2727def test_applymap_float_object_conversion (val ):
2828 # GH 2909: object conversion to float in constructor?
2929 df = DataFrame (data = [val , "a" ])
30- result = df .applymap (lambda x : x ).dtypes [0 ]
30+ result = df .map (lambda x : x ).dtypes [0 ]
3131 assert result == object
3232
3333
@@ -41,15 +41,15 @@ def test_applymap_keeps_dtype(na_action):
4141 def func (x ):
4242 return str .upper (x ) if not pd .isna (x ) else x
4343
44- result = df .applymap (func , na_action = na_action )
44+ result = df .map (func , na_action = na_action )
4545
4646 expected_sparse = pd .array (["A" , np .nan , "B" ], dtype = pd .SparseDtype (object ))
4747 expected_arr = expected_sparse .astype (object )
4848 expected = DataFrame ({"a" : expected_arr , "b" : expected_sparse })
4949
5050 tm .assert_frame_equal (result , expected )
5151
52- result_empty = df .iloc [:0 , :].applymap (func , na_action = na_action )
52+ result_empty = df .iloc [:0 , :].map (func , na_action = na_action )
5353 expected_empty = expected .iloc [:0 , :]
5454 tm .assert_frame_equal (result_empty , expected_empty )
5555
@@ -61,9 +61,9 @@ def test_applymap_str():
6161 cols = ["a" , "a" , "a" , "a" ]
6262 df .columns = cols
6363
64- expected = df2 .applymap (str )
64+ expected = df2 .map (str )
6565 expected .columns = cols
66- result = df .applymap (str )
66+ result = df .map (str )
6767 tm .assert_frame_equal (result , expected )
6868
6969
@@ -75,7 +75,7 @@ def test_applymap_datetimelike(col, val):
7575 # datetime/timedelta
7676 df = DataFrame (np .random .random ((3 , 4 )))
7777 df [col ] = val
78- result = df .applymap (str )
78+ result = df .map (str )
7979 assert result .loc [0 , col ] == str (df .loc [0 , col ])
8080
8181
@@ -91,24 +91,24 @@ def test_applymap_datetimelike(col, val):
9191@pytest .mark .parametrize ("func" , [round , lambda x : x ])
9292def test_applymap_empty (expected , func ):
9393 # GH 8222
94- result = expected .applymap (func )
94+ result = expected .map (func )
9595 tm .assert_frame_equal (result , expected )
9696
9797
9898def test_applymap_kwargs ():
9999 # 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 )
101101 expected = DataFrame ([[3 , 4 ], [5 , 6 ]])
102102 tm .assert_frame_equal (result , expected )
103103
104104
105105def test_applymap_na_ignore (float_frame ):
106106 # GH 23803
107- strlen_frame = float_frame .applymap (lambda x : len (str (x )))
107+ strlen_frame = float_frame .map (lambda x : len (str (x )))
108108 float_frame_with_na = float_frame .copy ()
109109 mask = np .random .randint (0 , 2 , size = float_frame .shape , dtype = bool )
110110 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 (
112112 lambda x : len (str (x )), na_action = "ignore"
113113 )
114114 strlen_frame_with_na = strlen_frame .copy ()
@@ -124,7 +124,7 @@ def func(x):
124124 return (x .hour , x .day , x .month )
125125
126126 # it works!
127- DataFrame (ser ).applymap (func )
127+ DataFrame (ser ).map (func )
128128
129129
130130def test_applymap_box ():
@@ -144,7 +144,7 @@ def test_applymap_box():
144144 }
145145 )
146146
147- result = df .applymap (lambda x : type (x ).__name__ )
147+ result = df .map (lambda x : type (x ).__name__ )
148148 expected = DataFrame (
149149 {
150150 "a" : ["Timestamp" , "Timestamp" ],
@@ -161,8 +161,8 @@ def test_frame_applymap_dont_convert_datetime64():
161161
162162 df = DataFrame ({"x1" : [datetime (1996 , 1 , 1 )]})
163163
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 ())
166166
167167 result = df .x1 .dtype
168168 assert result == "M8[ns]"
@@ -182,7 +182,7 @@ def non_reducing_function(val):
182182 for func in [reducing_function , non_reducing_function ]:
183183 del values [:]
184184
185- df .applymap (func )
185+ df .map (func )
186186 assert values == df .a .to_list ()
187187
188188
@@ -193,15 +193,23 @@ def test_applymap_type():
193193 index = ["a" , "b" , "c" ],
194194 )
195195
196- result = df .applymap (type )
196+ result = df .map (type )
197197 expected = DataFrame (
198198 {"col1" : [int , str , type ], "col2" : [float , datetime , float ]},
199199 index = ["a" , "b" , "c" ],
200200 )
201201 tm .assert_frame_equal (result , expected )
202202
203203
204- def test_applymap_invalid_na_action (float_frame ):
204+ def test_map_invalid_na_action (float_frame ):
205205 # GH 23803
206206 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