Skip to content

Commit 4e089d5

Browse files
Updating tests
1 parent 77efdfd commit 4e089d5

File tree

20 files changed

+187
-120
lines changed

20 files changed

+187
-120
lines changed

pandas/conftest.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,6 @@ def nselect_method(request):
402402
return request.param
403403

404404

405-
@pytest.fixture(params=[None, "ignore"])
406-
def na_action(request):
407-
"""
408-
Fixture for 'na_action' argument in map.
409-
"""
410-
return request.param
411-
412-
413405
@pytest.fixture(params=[True, False])
414406
def ascending(request):
415407
"""

pandas/core/frame.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10644,7 +10644,7 @@ def map(
1064410644
self,
1064510645
func: PythonFuncType,
1064610646
skipna: bool = False,
10647-
na_action: Literal["ignore"] | None = None,
10647+
na_action: Literal["ignore"] | None = lib.no_default,
1064810648
**kwargs,
1064910649
) -> DataFrame:
1065010650
"""
@@ -10725,14 +10725,20 @@ def map(
1072510725
0 1.000000 4.494400
1072610726
1 11.262736 20.857489
1072710727
"""
10728-
if na_action == "ignore":
10728+
if na_action != lib.no_default:
1072910729
warnings.warn(
1073010730
"The ``na_action`` parameter has been deprecated and it will be "
1073110731
"removed in a future version of pandas. Use ``skipna`` instead.",
1073210732
FutureWarning,
1073310733
stacklevel=find_stack_level(),
1073410734
)
10735-
skipna = True
10735+
if na_action == "ignore":
10736+
skipna = True
10737+
elif na_action not in (None, "ignore"):
10738+
raise ValueError(
10739+
"na_action must either be 'ignore' or None, "
10740+
f"{na_action!r} was passed"
10741+
)
1073610742

1073710743
if self.empty:
1073810744
return self.copy()

pandas/core/indexes/base.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6351,7 +6351,10 @@ def groupby(self, values) -> PrettyDict[Hashable, Index]:
63516351
return PrettyDict(result)
63526352

63536353
def map(
6354-
self, mapper, skipna: bool = False, na_action: Literal["ignore"] | None = None
6354+
self,
6355+
mapper,
6356+
skipna: bool = False,
6357+
na_action: Literal["ignore"] | None = lib.no_default,
63556358
):
63566359
"""
63576360
Map values using an input mapping or function.
@@ -6399,14 +6402,20 @@ def map(
63996402
"""
64006403
from pandas.core.indexes.multi import MultiIndex
64016404

6402-
if na_action == "ignore":
6405+
if na_action != lib.no_default:
64036406
warnings.warn(
64046407
"The ``na_action`` parameter has been deprecated and it will be "
64056408
"removed in a future version of pandas. Use ``skipna`` instead.",
64066409
FutureWarning,
64076410
stacklevel=find_stack_level(),
64086411
)
6409-
skipna = True
6412+
if na_action == "ignore":
6413+
skipna = True
6414+
elif na_action not in (None, "ignore"):
6415+
raise ValueError(
6416+
"na_action must either be 'ignore' or None, "
6417+
f"{na_action!r} was passed"
6418+
)
64106419

64116420
new_values = self._map_values(mapper, skipna=skipna)
64126421

pandas/core/series.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4328,7 +4328,7 @@ def map(
43284328
self,
43294329
func: Callable | Mapping | Series | None = None,
43304330
skipna: bool = False,
4331-
na_action: Literal["ignore"] | None = None,
4331+
na_action: Literal["ignore"] | None = lib.no_default,
43324332
engine: Callable | None = None,
43334333
**kwargs,
43344334
) -> Series:
@@ -4437,14 +4437,20 @@ def map(
44374437
3 I am a rabbit
44384438
dtype: object
44394439
"""
4440-
if na_action == "ignore":
4440+
if na_action != lib.no_default:
44414441
warnings.warn(
44424442
"The ``na_action`` parameter has been deprecated and it will be "
44434443
"removed in a future version of pandas. Use ``skipna`` instead.",
44444444
FutureWarning,
44454445
stacklevel=find_stack_level(),
44464446
)
4447-
skipna = True
4447+
if na_action == "ignore":
4448+
skipna = True
4449+
elif na_action not in (None, "ignore"):
4450+
raise ValueError(
4451+
"na_action must either be 'ignore' or None, "
4452+
f"{na_action!r} was passed"
4453+
)
44484454

44494455
if func is None:
44504456
if "arg" in kwargs:

pandas/tests/apply/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MockExecutionEngine(BaseExecutionEngine):
2323

2424
def map(data, func, args, kwargs, decorator, skip_na):
2525
kwargs_to_pass = kwargs if isinstance(data, DataFrame) else {}
26-
return data.map(func, na_action="ignore" if skip_na else None, **kwargs_to_pass)
26+
return data.map(func, skipna=skip_na, **kwargs_to_pass)
2727

2828
def apply(data, func, args, kwargs, decorator, axis):
2929
if isinstance(data, Series):

pandas/tests/apply/test_invalid_arg.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ def test_agg_raises():
5454
df.agg()
5555

5656

57-
def test_map_with_invalid_na_action_raises():
58-
# https://github.com/pandas-dev/pandas/issues/32815
59-
s = Series([1, 2, 3])
60-
msg = "na_action must either be 'ignore' or None"
61-
with pytest.raises(ValueError, match=msg):
62-
s.map(lambda x: x, na_action="____")
63-
64-
65-
@pytest.mark.parametrize("input_na_action", ["____", True])
66-
def test_map_arg_is_dict_with_invalid_na_action_raises(input_na_action):
67-
# https://github.com/pandas-dev/pandas/issues/46588
68-
s = Series([1, 2, 3])
69-
msg = f"na_action must either be 'ignore' or None, {input_na_action} was passed"
70-
with pytest.raises(ValueError, match=msg):
71-
s.map({1: 2}, na_action=input_na_action)
72-
73-
7457
@pytest.mark.parametrize("method", ["apply", "agg", "transform"])
7558
@pytest.mark.parametrize("func", [{"A": {"B": "sum"}}, {"A": {"B": ["sum"]}}])
7659
def test_nested_renamer(frame_or_series, method, func):

pandas/tests/arrays/categorical/test_analytics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,16 @@ def test_memory_usage(self, using_infer_string):
318318

319319
def test_map(self):
320320
c = Categorical(list("ABABC"), categories=list("CBA"), ordered=True)
321-
result = c.map(lambda x: x.lower(), na_action=None)
321+
result = c.map(lambda x: x.lower(), skipna=False)
322322
exp = Categorical(list("ababc"), categories=list("cba"), ordered=True)
323323
tm.assert_categorical_equal(result, exp)
324324

325325
c = Categorical(list("ABABC"), categories=list("ABC"), ordered=False)
326-
result = c.map(lambda x: x.lower(), na_action=None)
326+
result = c.map(lambda x: x.lower(), skipna=False)
327327
exp = Categorical(list("ababc"), categories=list("abc"), ordered=False)
328328
tm.assert_categorical_equal(result, exp)
329329

330-
result = c.map(lambda x: 1, na_action=None)
330+
result = c.map(lambda x: 1, skipna=False)
331331
# GH 12766: Return an index not an array
332332
tm.assert_index_equal(result, Index(np.array([1] * 5, dtype=np.int64)))
333333

pandas/tests/arrays/categorical/test_map.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@
1818
],
1919
ids=["string", "interval"],
2020
)
21-
def test_map_str(data, categories, ordered, na_action):
21+
def test_map_str(data, categories, ordered, skipna):
2222
# GH 31202 - override base class since we want to maintain categorical/ordered
2323
cat = Categorical(data, categories=categories, ordered=ordered)
24-
result = cat.map(str, na_action=na_action)
24+
result = cat.map(str, skipna=skipna)
2525
expected = Categorical(
2626
map(str, data), categories=map(str, categories), ordered=ordered
2727
)
2828
tm.assert_categorical_equal(result, expected)
2929

3030

31-
def test_map(na_action):
31+
def test_map(skipna):
3232
cat = Categorical(list("ABABC"), categories=list("CBA"), ordered=True)
33-
result = cat.map(lambda x: x.lower(), na_action=na_action)
33+
result = cat.map(lambda x: x.lower(), skipna=skipna)
3434
exp = Categorical(list("ababc"), categories=list("cba"), ordered=True)
3535
tm.assert_categorical_equal(result, exp)
3636

3737
cat = Categorical(list("ABABC"), categories=list("BAC"), ordered=False)
38-
result = cat.map(lambda x: x.lower(), na_action=na_action)
38+
result = cat.map(lambda x: x.lower(), skipna=skipna)
3939
exp = Categorical(list("ababc"), categories=list("bac"), ordered=False)
4040
tm.assert_categorical_equal(result, exp)
4141

4242
# GH 12766: Return an index not an array
43-
result = cat.map(lambda x: 1, na_action=na_action)
43+
result = cat.map(lambda x: 1, skipna=skipna)
4444
exp = Index(np.array([1] * 5, dtype=np.int64))
4545
tm.assert_index_equal(result, exp)
4646

@@ -50,15 +50,15 @@ def test_map(na_action):
5050
def f(x):
5151
return {"A": 10, "B": 20, "C": 30}.get(x)
5252

53-
result = cat.map(f, na_action=na_action)
53+
result = cat.map(f, skipna=skipna)
5454
exp = Categorical([10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False)
5555
tm.assert_categorical_equal(result, exp)
5656

5757
mapper = Series([10, 20, 30], index=["A", "B", "C"])
58-
result = cat.map(mapper, na_action=na_action)
58+
result = cat.map(mapper, skipna=skipna)
5959
tm.assert_categorical_equal(result, exp)
6060

61-
result = cat.map({"A": 10, "B": 20, "C": 30}, na_action=na_action)
61+
result = cat.map({"A": 10, "B": 20, "C": 30}, skipna=skipna)
6262
tm.assert_categorical_equal(result, exp)
6363

6464

@@ -83,7 +83,7 @@ def f(x):
8383
)
8484
def test_map_with_nan_none(data, f, expected): # GH 24241
8585
values = Categorical(data)
86-
result = values.map(f, na_action=None)
86+
result = values.map(f, skipna=False)
8787
if isinstance(expected, Categorical):
8888
tm.assert_categorical_equal(result, expected)
8989
else:
@@ -111,26 +111,26 @@ def test_map_with_nan_none(data, f, expected): # GH 24241
111111
)
112112
def test_map_with_nan_ignore(data, f, expected): # GH 24241
113113
values = Categorical(data)
114-
result = values.map(f, na_action="ignore")
114+
result = values.map(f, skipna=True)
115115
if data[1] == 1:
116116
tm.assert_categorical_equal(result, expected)
117117
else:
118118
tm.assert_index_equal(result, expected)
119119

120120

121-
def test_map_with_dict_or_series(na_action):
121+
def test_map_with_dict_or_series(skipna):
122122
orig_values = ["a", "B", 1, "a"]
123123
new_values = ["one", 2, 3.0, "one"]
124124
cat = Categorical(orig_values)
125125

126126
mapper = Series(new_values[:-1], index=orig_values[:-1])
127-
result = cat.map(mapper, na_action=na_action)
127+
result = cat.map(mapper, skipna=skipna)
128128

129129
# Order of categories in result can be different
130130
expected = Categorical(new_values, categories=[3.0, 2, "one"])
131131
tm.assert_categorical_equal(result, expected)
132132

133133
mapper = dict(zip(orig_values[:-1], new_values[:-1]))
134-
result = cat.map(mapper, na_action=na_action)
134+
result = cat.map(mapper, skipna=skipna)
135135
# Order of categories in result can be different
136136
tm.assert_categorical_equal(result, expected)

pandas/tests/arrays/categorical/test_subclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_from_codes(self):
2020

2121
def test_map(self):
2222
sc = SubclassedCategorical(["a", "b", "c"])
23-
res = sc.map(lambda x: x.upper(), na_action=None)
23+
res = sc.map(lambda x: x.upper(), skipna=False)
2424
assert isinstance(res, SubclassedCategorical)
2525
exp = Categorical(["A", "B", "C"])
2626
tm.assert_categorical_equal(res, exp)

pandas/tests/extension/base/methods.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def test_apply_simple_series(self, data):
9797
result = pd.Series(data).apply(id)
9898
assert isinstance(result, pd.Series)
9999

100-
@pytest.mark.parametrize("na_action", [None, "ignore"])
101-
def test_map(self, data_missing, na_action):
102-
result = data_missing.map(lambda x: x, na_action=na_action)
100+
@pytest.mark.parametrize("skipna", [False, True])
101+
def test_map(self, data_missing, skipna):
102+
result = data_missing.map(lambda x: x, skipna=skipna)
103103
expected = data_missing.to_numpy()
104104
tm.assert_numpy_array_equal(result, expected)
105105

0 commit comments

Comments
 (0)