Skip to content

Commit 06dcad6

Browse files
committed
passed series mask tests
1 parent 671cf86 commit 06dcad6

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM quay.io/condaforge/miniforge3
22

33
# if you forked pandas, you can pass in your own GitHub username to use your fork
44
# i.e. gh_username=myname
5-
ARG gh_username=pandas-dev
5+
ARG gh_username=ShreyDixit
66
ARG pandas_home="/home/pandas"
77

88
# Avoid warnings by switching to noninteractive

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ Deprecations
648648
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
649649
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
650650
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
651+
- Deprecated passing arguments (apart from ``cond``) as positional in :meth:`DataFrame.mask` (:issue:`41485`)
651652

652653
.. ---------------------------------------------------------------------------
653654

pandas/core/generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
)
6363
from pandas.util._decorators import (
6464
doc,
65+
deprecate_nonkeyword_arguments,
6566
rewrite_axis_style_signature,
6667
)
6768
from pandas.util._validators import (
@@ -9231,6 +9232,7 @@ def where(
92319232
name="mask",
92329233
name_other="where",
92339234
)
9235+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "cond"])
92349236
def mask(
92359237
self,
92369238
cond,

pandas/tests/frame/indexing/test_mask.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def test_mask(self):
2525

2626
other = DataFrame(np.random.randn(5, 3))
2727
rs = df.where(cond, other)
28-
tm.assert_frame_equal(rs, df.mask(df <= 0, other))
29-
tm.assert_frame_equal(rs, df.mask(~cond, other))
28+
tm.assert_frame_equal(rs, df.mask(df <= 0, other=other))
29+
tm.assert_frame_equal(rs, df.mask(~cond, other=other))
3030

3131
# see GH#21891
3232
df = DataFrame([1, 2])
@@ -51,7 +51,7 @@ def test_mask_inplace(self):
5151
return_value = rdf.where(cond, -df, inplace=True)
5252
assert return_value is None
5353
tm.assert_frame_equal(rdf, df.where(cond, -df))
54-
tm.assert_frame_equal(rdf, df.mask(~cond, -df))
54+
tm.assert_frame_equal(rdf, df.mask(~cond, other=-df))
5555

5656
def test_mask_edge_case_1xN_frame(self):
5757
# GH#4071
@@ -63,22 +63,22 @@ def test_mask_edge_case_1xN_frame(self):
6363
def test_mask_callable(self):
6464
# GH#12533
6565
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
66-
result = df.mask(lambda x: x > 4, lambda x: x + 1)
66+
result = df.mask(lambda x: x > 4, other=lambda x: x + 1)
6767
exp = DataFrame([[1, 2, 3], [4, 6, 7], [8, 9, 10]])
6868
tm.assert_frame_equal(result, exp)
69-
tm.assert_frame_equal(result, df.mask(df > 4, df + 1))
69+
tm.assert_frame_equal(result, df.mask(df > 4, other=df + 1))
7070

7171
# return ndarray and scalar
72-
result = df.mask(lambda x: (x % 2 == 0).values, lambda x: 99)
72+
result = df.mask(lambda x: (x % 2 == 0).values, other=lambda x: 99)
7373
exp = DataFrame([[1, 99, 3], [99, 5, 99], [7, 99, 9]])
7474
tm.assert_frame_equal(result, exp)
75-
tm.assert_frame_equal(result, df.mask(df % 2 == 0, 99))
75+
tm.assert_frame_equal(result, df.mask(df % 2 == 0, other=99))
7676

7777
# chain
78-
result = (df + 2).mask(lambda x: x > 8, lambda x: x + 10)
78+
result = (df + 2).mask(lambda x: x > 8, other=lambda x: x + 10)
7979
exp = DataFrame([[3, 4, 5], [6, 7, 8], [19, 20, 21]])
8080
tm.assert_frame_equal(result, exp)
81-
tm.assert_frame_equal(result, (df + 2).mask((df + 2) > 8, (df + 2) + 10))
81+
tm.assert_frame_equal(result, (df + 2).mask((df + 2) > 8, other=(df + 2) + 10))
8282

8383
def test_mask_dtype_bool_conversion(self):
8484
# GH#3733
@@ -90,6 +90,21 @@ def test_mask_dtype_bool_conversion(self):
9090
result = bools.mask(mask)
9191
tm.assert_frame_equal(result, expected)
9292

93+
def test_mask_pos_args_deprecation(self):
94+
# https://github.com/pandas-dev/pandas/issues/41485
95+
df = DataFrame(np.random.randn(5, 5))
96+
cond = df > 0
97+
other = DataFrame(np.random.randn(5, 3))
98+
99+
msg = (
100+
r"Starting with Pandas version 2\.0 all arguments of mask except for the "
101+
r"arguments 'self' and 'cond' will be keyword-only"
102+
)
103+
with tm.assert_produces_warning(FutureWarning, match=msg):
104+
df.mask(cond, other)
105+
106+
# tm.assert_frame_equal(df.mask(cond, other), df.mask(cond, other=other))
107+
93108

94109
def test_mask_try_cast_deprecated(frame_or_series):
95110

@@ -101,7 +116,7 @@ def test_mask_try_cast_deprecated(frame_or_series):
101116

102117
with tm.assert_produces_warning(FutureWarning):
103118
# try_cast keyword deprecated
104-
obj.mask(mask, -1, try_cast=True)
119+
obj.mask(mask, other=-1, try_cast=True)
105120

106121

107122
def test_mask_stringdtype():
@@ -115,11 +130,11 @@ def test_mask_stringdtype():
115130
{"A": ["this", "that"]}, index=["id2", "id3"], dtype=StringDtype()
116131
)
117132
filter_ser = Series([False, True, True, False])
118-
result = df.mask(filter_ser, filtered_df)
133+
result = df.mask(filter_ser, other=filtered_df)
119134

120135
expected = DataFrame(
121136
{"A": [NA, "this", "that", NA]},
122137
index=["id1", "id2", "id3", "id4"],
123138
dtype=StringDtype(),
124139
)
125-
tm.assert_frame_equal(result, expected)
140+
tm.assert_frame_equal(result, expected)

pandas/tests/series/indexing/test_mask.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_mask():
2222
tm.assert_series_equal(rs, rs2)
2323

2424
rs = s.where(~cond, -s)
25-
rs2 = s.mask(cond, -s)
25+
rs2 = s.mask(cond, other=-s)
2626
tm.assert_series_equal(rs, rs2)
2727

2828
cond = Series([True, False, False, True, False], index=s.index)
@@ -32,18 +32,18 @@ def test_mask():
3232
tm.assert_series_equal(rs, rs2)
3333

3434
rs = s2.where(~cond[:3], -s2)
35-
rs2 = s2.mask(cond[:3], -s2)
35+
rs2 = s2.mask(cond[:3], other=-s2)
3636
tm.assert_series_equal(rs, rs2)
3737

3838
msg = "Array conditional must be same shape as self"
3939
with pytest.raises(ValueError, match=msg):
4040
s.mask(1)
4141
with pytest.raises(ValueError, match=msg):
42-
s.mask(cond[:3].values, -s)
42+
s.mask(cond[:3].values, other=-s)
4343

4444
# dtype changes
4545
s = Series([1, 2, 3, 4])
46-
result = s.mask(s > 2, np.nan)
46+
result = s.mask(s > 2, other=np.nan)
4747
expected = Series([1, 2, np.nan, np.nan])
4848
tm.assert_series_equal(result, expected)
4949

@@ -65,8 +65,8 @@ def test_mask_inplace():
6565
tm.assert_series_equal(rs, s.mask(cond))
6666

6767
rs = s.copy()
68-
rs.mask(cond, -s, inplace=True)
69-
tm.assert_series_equal(rs, s.mask(cond, -s))
68+
rs.mask(cond, other=-s, inplace=True)
69+
tm.assert_series_equal(rs, s.mask(cond, other=-s))
7070

7171

7272
def test_mask_stringdtype():
@@ -78,11 +78,24 @@ def test_mask_stringdtype():
7878
)
7979
filtered_ser = Series(["this", "that"], index=["id2", "id3"], dtype=StringDtype())
8080
filter_ser = Series([False, True, True, False])
81-
result = ser.mask(filter_ser, filtered_ser)
81+
result = ser.mask(filter_ser, other=filtered_ser)
8282

8383
expected = Series(
8484
[NA, "this", "that", NA],
8585
index=["id1", "id2", "id3", "id4"],
8686
dtype=StringDtype(),
8787
)
8888
tm.assert_series_equal(result, expected)
89+
90+
91+
def test_mask_pos_args_deprecation():
92+
# https://github.com/pandas-dev/pandas/issues/41485
93+
df = Series(np.random.randn(6))
94+
cond = df > 0
95+
96+
msg = (
97+
r"Starting with Pandas version 2\.0 all arguments of mask except for the "
98+
r"arguments 'self' and 'cond' will be keyword-only"
99+
)
100+
with tm.assert_produces_warning(FutureWarning, match=msg):
101+
df.mask(cond, np.nan)

pandas/tests/series/indexing/test_where.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def test_broadcast(size, mask, item, box):
314314
tm.assert_series_equal(result, expected)
315315

316316
s = Series(data)
317-
result = s.mask(selection, box(item))
317+
result = s.mask(selection, other=box(item))
318318
tm.assert_series_equal(result, expected)
319319

320320

path_to_file.xlsx

5.45 KB
Binary file not shown.

0 commit comments

Comments
 (0)