Skip to content

Commit eef145d

Browse files
Jiezheng2018TLouf
authored andcommitted
added deprecate_nonkeyword_arguments to function where (pandas-dev#41523)
1 parent 4c0a490 commit eef145d

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ Deprecations
683683
- Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``), :meth:`Series.drop_duplicates`, :meth:`Index.drop_duplicates` and :meth:`MultiIndex.drop_duplicates`(:issue:`41485`)
684684
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
685685
- Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`)
686+
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
686687

687688
.. _whatsnew_130.deprecations.nuisance_columns:
688689

pandas/core/frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -10689,6 +10689,21 @@ def interpolate(
1068910689
**kwargs,
1069010690
)
1069110691

10692+
@deprecate_nonkeyword_arguments(
10693+
version=None, allowed_args=["self", "cond", "other"]
10694+
)
10695+
def where(
10696+
self,
10697+
cond,
10698+
other=np.nan,
10699+
inplace=False,
10700+
axis=None,
10701+
level=None,
10702+
errors="raise",
10703+
try_cast=lib.no_default,
10704+
):
10705+
return super().where(cond, other, inplace, axis, level, errors, try_cast)
10706+
1069210707

1069310708
DataFrame._add_numeric_operations()
1069410709

pandas/core/generic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9073,7 +9073,6 @@ def _where(
90739073
result = self._constructor(new_data)
90749074
return result.__finalize__(self)
90759075

9076-
@final
90779076
@doc(
90789077
klass=_shared_doc_kwargs["klass"],
90799078
cond="True",
@@ -9221,7 +9220,7 @@ def where(
92219220
"try_cast keyword is deprecated and will be removed in a "
92229221
"future version",
92239222
FutureWarning,
9224-
stacklevel=2,
9223+
stacklevel=4,
92259224
)
92269225

92279226
return self._where(cond, other, inplace, axis, level, errors=errors)

pandas/core/series.py

+15
Original file line numberDiff line numberDiff line change
@@ -5311,6 +5311,21 @@ def interpolate(
53115311
**kwargs,
53125312
)
53135313

5314+
@deprecate_nonkeyword_arguments(
5315+
version=None, allowed_args=["self", "cond", "other"]
5316+
)
5317+
def where(
5318+
self,
5319+
cond,
5320+
other=np.nan,
5321+
inplace=False,
5322+
axis=None,
5323+
level=None,
5324+
errors="raise",
5325+
try_cast=lib.no_default,
5326+
):
5327+
return super().where(cond, other, inplace, axis, level, errors, try_cast)
5328+
53145329
# ----------------------------------------------------------------------
53155330
# Add index
53165331
_AXIS_ORDERS = ["index"]

pandas/tests/frame/indexing/test_where.py

+14
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,17 @@ def test_where_none_nan_coerce():
757757
)
758758
result = expected.where(expected.notnull(), None)
759759
tm.assert_frame_equal(result, expected)
760+
761+
762+
def test_where_non_keyword_deprecation():
763+
# GH 41485
764+
s = DataFrame(range(5))
765+
msg = (
766+
"In a future version of pandas all arguments of "
767+
"DataFrame.where except for the arguments 'cond' "
768+
"and 'other' will be keyword-only"
769+
)
770+
with tm.assert_produces_warning(FutureWarning, match=msg):
771+
result = s.where(s > 1, 10, False)
772+
expected = DataFrame([10, 10, 2, 3, 4])
773+
tm.assert_frame_equal(expected, result)

pandas/tests/series/indexing/test_where.py

+14
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ def test_where():
141141
tm.assert_series_equal(rs, expected)
142142

143143

144+
def test_where_non_keyword_deprecation():
145+
# GH 41485
146+
s = Series(range(5))
147+
msg = (
148+
"In a future version of pandas all arguments of "
149+
"Series.where except for the arguments 'cond' "
150+
"and 'other' will be keyword-only"
151+
)
152+
with tm.assert_produces_warning(FutureWarning, match=msg):
153+
result = s.where(s > 1, 10, False)
154+
expected = Series([10, 10, 2, 3, 4])
155+
tm.assert_series_equal(expected, result)
156+
157+
144158
def test_where_error():
145159
s = Series(np.random.randn(5))
146160
cond = s > 0

0 commit comments

Comments
 (0)