11from datetime import datetime
2+ import warnings
23
34import numpy as np
45import pytest
1314import pandas ._testing as tm
1415
1516
16- def test_applymap (float_frame ):
17- result = float_frame .applymap (lambda x : x * 2 )
17+ def test_map (float_frame ):
18+ result = float_frame .map (lambda x : x * 2 )
1819 tm .assert_frame_equal (result , float_frame * 2 )
19- float_frame .applymap (type )
20+ float_frame .map (type )
2021
2122 # GH 465: function returning tuples
22- result = float_frame .applymap (lambda x : (x , x ))["A" ][0 ]
23+ result = float_frame .map (lambda x : (x , x ))["A" ][0 ]
2324 assert isinstance (result , tuple )
2425
2526
2627@pytest .mark .parametrize ("val" , [1 , 1.0 ])
2728def test_applymap_float_object_conversion (val ):
2829 # GH 2909: object conversion to float in constructor?
2930 df = DataFrame (data = [val , "a" ])
30- result = df .applymap (lambda x : x ).dtypes [0 ]
31+ result = df .map (lambda x : x ).dtypes [0 ]
3132 assert result == object
3233
3334
@@ -41,15 +42,15 @@ def test_applymap_keeps_dtype(na_action):
4142 def func (x ):
4243 return str .upper (x ) if not pd .isna (x ) else x
4344
44- result = df .applymap (func , na_action = na_action )
45+ result = df .map (func , na_action = na_action )
4546
4647 expected_sparse = pd .array (["A" , np .nan , "B" ], dtype = pd .SparseDtype (object ))
4748 expected_arr = expected_sparse .astype (object )
4849 expected = DataFrame ({"a" : expected_arr , "b" : expected_sparse })
4950
5051 tm .assert_frame_equal (result , expected )
5152
52- result_empty = df .iloc [:0 , :].applymap (func , na_action = na_action )
53+ result_empty = df .iloc [:0 , :].map (func , na_action = na_action )
5354 expected_empty = expected .iloc [:0 , :]
5455 tm .assert_frame_equal (result_empty , expected_empty )
5556
@@ -61,9 +62,9 @@ def test_applymap_str():
6162 cols = ["a" , "a" , "a" , "a" ]
6263 df .columns = cols
6364
64- expected = df2 .applymap (str )
65+ expected = df2 .map (str )
6566 expected .columns = cols
66- result = df .applymap (str )
67+ result = df .map (str )
6768 tm .assert_frame_equal (result , expected )
6869
6970
@@ -75,7 +76,7 @@ def test_applymap_datetimelike(col, val):
7576 # datetime/timedelta
7677 df = DataFrame (np .random .random ((3 , 4 )))
7778 df [col ] = val
78- result = df .applymap (str )
79+ result = df .map (str )
7980 assert result .loc [0 , col ] == str (df .loc [0 , col ])
8081
8182
@@ -91,24 +92,24 @@ def test_applymap_datetimelike(col, val):
9192@pytest .mark .parametrize ("func" , [round , lambda x : x ])
9293def test_applymap_empty (expected , func ):
9394 # GH 8222
94- result = expected .applymap (func )
95+ result = expected .map (func )
9596 tm .assert_frame_equal (result , expected )
9697
9798
9899def test_applymap_kwargs ():
99100 # GH 40652
100- result = DataFrame ([[1 , 2 ], [3 , 4 ]]).applymap (lambda x , y : x + y , y = 2 )
101+ result = DataFrame ([[1 , 2 ], [3 , 4 ]]).map (lambda x , y : x + y , y = 2 )
101102 expected = DataFrame ([[3 , 4 ], [5 , 6 ]])
102103 tm .assert_frame_equal (result , expected )
103104
104105
105106def test_applymap_na_ignore (float_frame ):
106107 # GH 23803
107- strlen_frame = float_frame .applymap (lambda x : len (str (x )))
108+ strlen_frame = float_frame .map (lambda x : len (str (x )))
108109 float_frame_with_na = float_frame .copy ()
109110 mask = np .random .randint (0 , 2 , size = float_frame .shape , dtype = bool )
110111 float_frame_with_na [mask ] = pd .NA
111- strlen_frame_na_ignore = float_frame_with_na .applymap (
112+ strlen_frame_na_ignore = float_frame_with_na .map (
112113 lambda x : len (str (x )), na_action = "ignore"
113114 )
114115 strlen_frame_with_na = strlen_frame .copy ()
@@ -124,7 +125,7 @@ def func(x):
124125 return (x .hour , x .day , x .month )
125126
126127 # it works!
127- DataFrame (ser ).applymap (func )
128+ DataFrame (ser ).map (func )
128129
129130
130131def test_applymap_box ():
@@ -144,7 +145,7 @@ def test_applymap_box():
144145 }
145146 )
146147
147- result = df .applymap (lambda x : type (x ).__name__ )
148+ result = df .map (lambda x : type (x ).__name__ )
148149 expected = DataFrame (
149150 {
150151 "a" : ["Timestamp" , "Timestamp" ],
@@ -161,8 +162,8 @@ def test_frame_applymap_dont_convert_datetime64():
161162
162163 df = DataFrame ({"x1" : [datetime (1996 , 1 , 1 )]})
163164
164- df = df .applymap (lambda x : x + BDay ())
165- df = df .applymap (lambda x : x + BDay ())
165+ df = df .map (lambda x : x + BDay ())
166+ df = df .map (lambda x : x + BDay ())
166167
167168 result = df .x1 .dtype
168169 assert result == "M8[ns]"
@@ -182,7 +183,7 @@ def non_reducing_function(val):
182183 for func in [reducing_function , non_reducing_function ]:
183184 del values [:]
184185
185- df .applymap (func )
186+ df .map (func )
186187 assert values == df .a .to_list ()
187188
188189
@@ -193,15 +194,23 @@ def test_applymap_type():
193194 index = ["a" , "b" , "c" ],
194195 )
195196
196- result = df .applymap (type )
197+ result = df .map (type )
197198 expected = DataFrame (
198199 {"col1" : [int , str , type ], "col2" : [float , datetime , float ]},
199200 index = ["a" , "b" , "c" ],
200201 )
201202 tm .assert_frame_equal (result , expected )
202203
203204
204- def test_applymap_invalid_na_action (float_frame ):
205+ def test_map_invalid_na_action (float_frame ):
205206 # GH 23803
206207 with pytest .raises (ValueError , match = "na_action must be .*Got 'abc'" ):
207- float_frame .applymap (lambda x : len (str (x )), na_action = "abc" )
208+ float_frame .map (lambda x : len (str (x )), na_action = "abc" )
209+
210+
211+ def test_applymap_deprecated ():
212+ # GH52353
213+ df = DataFrame ({"a" : [1 , 2 , 3 ]})
214+ msg = "DataFrame.applymap has been deprecated. Use DataFrame.map instead."
215+ with tm .assert_produces_warning (FutureWarning , match = msg ):
216+ df .applymap (lambda x : x )
0 commit comments