Skip to content

Commit 281dcc3

Browse files
ShreyDixitTLouf
authored andcommitted
Deprecate non-keyword arguments in mask (pandas-dev#41580)
1 parent 44dc8c3 commit 281dcc3

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ Deprecations
680680
- 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`)
681681
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
682682
- Deprecated passing arguments as positional in :meth:`Index.set_names` and :meth:`MultiIndex.set_names` (except for ``names``) (:issue:`41485`)
683+
- Deprecated passing arguments (apart from ``cond`` and ``other``) as positional in :meth:`DataFrame.mask` and :meth:`Series.mask` (:issue:`41485`)
683684
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
684685
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
685686
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)

pandas/core/frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -10720,6 +10720,21 @@ def where(
1072010720
):
1072110721
return super().where(cond, other, inplace, axis, level, errors, try_cast)
1072210722

10723+
@deprecate_nonkeyword_arguments(
10724+
version=None, allowed_args=["self", "cond", "other"]
10725+
)
10726+
def mask(
10727+
self,
10728+
cond,
10729+
other=np.nan,
10730+
inplace=False,
10731+
axis=None,
10732+
level=None,
10733+
errors="raise",
10734+
try_cast=lib.no_default,
10735+
):
10736+
return super().mask(cond, other, inplace, axis, level, errors, try_cast)
10737+
1072310738

1072410739
DataFrame._add_numeric_operations()
1072510740

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9062,7 +9062,7 @@ def mask(
90629062
"try_cast keyword is deprecated and will be removed in a "
90639063
"future version",
90649064
FutureWarning,
9065-
stacklevel=2,
9065+
stacklevel=4,
90669066
)
90679067

90689068
# see gh-21891

pandas/core/series.py

+15
Original file line numberDiff line numberDiff line change
@@ -5366,6 +5366,21 @@ def where(
53665366
):
53675367
return super().where(cond, other, inplace, axis, level, errors, try_cast)
53685368

5369+
@deprecate_nonkeyword_arguments(
5370+
version=None, allowed_args=["self", "cond", "other"]
5371+
)
5372+
def mask(
5373+
self,
5374+
cond,
5375+
other=np.nan,
5376+
inplace=False,
5377+
axis=None,
5378+
level=None,
5379+
errors="raise",
5380+
try_cast=lib.no_default,
5381+
):
5382+
return super().mask(cond, other, inplace, axis, level, errors, try_cast)
5383+
53695384
# ----------------------------------------------------------------------
53705385
# Add index
53715386
_AXIS_ORDERS = ["index"]

pandas/tests/frame/indexing/test_mask.py

+13
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ 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({"a": range(5)})
96+
expected = DataFrame({"a": [-1, 1, -1, 3, -1]})
97+
cond = df % 2 == 0
98+
msg = (
99+
r"In a future version of pandas all arguments of DataFrame.mask except for "
100+
r"the arguments 'cond' and 'other' will be keyword-only"
101+
)
102+
with tm.assert_produces_warning(FutureWarning, match=msg):
103+
result = df.mask(cond, -1, False)
104+
tm.assert_frame_equal(result, expected)
105+
93106

94107
def test_mask_try_cast_deprecated(frame_or_series):
95108

pandas/tests/series/indexing/test_mask.py

+14
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,17 @@ def test_mask_stringdtype():
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+
s = Series(range(5))
94+
expected = Series([-1, 1, -1, 3, -1])
95+
cond = s % 2 == 0
96+
msg = (
97+
r"In a future version of pandas all arguments of Series.mask except for "
98+
r"the arguments 'cond' and 'other' will be keyword-only"
99+
)
100+
with tm.assert_produces_warning(FutureWarning, match=msg):
101+
result = s.mask(cond, -1, False)
102+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)