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