Skip to content

Commit

Permalink
added deprecate_nonkeyword_arguments to function where (pandas-dev#41523
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Jiezheng2018 authored and TLouf committed Jun 1, 2021
1 parent 4c0a490 commit eef145d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ Deprecations
- 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`)
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)

.. _whatsnew_130.deprecations.nuisance_columns:

Expand Down
15 changes: 15 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10689,6 +10689,21 @@ def interpolate(
**kwargs,
)

@deprecate_nonkeyword_arguments(
version=None, allowed_args=["self", "cond", "other"]
)
def where(
self,
cond,
other=np.nan,
inplace=False,
axis=None,
level=None,
errors="raise",
try_cast=lib.no_default,
):
return super().where(cond, other, inplace, axis, level, errors, try_cast)


DataFrame._add_numeric_operations()

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9073,7 +9073,6 @@ def _where(
result = self._constructor(new_data)
return result.__finalize__(self)

@final
@doc(
klass=_shared_doc_kwargs["klass"],
cond="True",
Expand Down Expand Up @@ -9221,7 +9220,7 @@ def where(
"try_cast keyword is deprecated and will be removed in a "
"future version",
FutureWarning,
stacklevel=2,
stacklevel=4,
)

return self._where(cond, other, inplace, axis, level, errors=errors)
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5311,6 +5311,21 @@ def interpolate(
**kwargs,
)

@deprecate_nonkeyword_arguments(
version=None, allowed_args=["self", "cond", "other"]
)
def where(
self,
cond,
other=np.nan,
inplace=False,
axis=None,
level=None,
errors="raise",
try_cast=lib.no_default,
):
return super().where(cond, other, inplace, axis, level, errors, try_cast)

# ----------------------------------------------------------------------
# Add index
_AXIS_ORDERS = ["index"]
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,17 @@ def test_where_none_nan_coerce():
)
result = expected.where(expected.notnull(), None)
tm.assert_frame_equal(result, expected)


def test_where_non_keyword_deprecation():
# GH 41485
s = DataFrame(range(5))
msg = (
"In a future version of pandas all arguments of "
"DataFrame.where except for the arguments 'cond' "
"and 'other' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.where(s > 1, 10, False)
expected = DataFrame([10, 10, 2, 3, 4])
tm.assert_frame_equal(expected, result)
14 changes: 14 additions & 0 deletions pandas/tests/series/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ def test_where():
tm.assert_series_equal(rs, expected)


def test_where_non_keyword_deprecation():
# GH 41485
s = Series(range(5))
msg = (
"In a future version of pandas all arguments of "
"Series.where except for the arguments 'cond' "
"and 'other' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.where(s > 1, 10, False)
expected = Series([10, 10, 2, 3, 4])
tm.assert_series_equal(expected, result)


def test_where_error():
s = Series(np.random.randn(5))
cond = s > 0
Expand Down

0 comments on commit eef145d

Please sign in to comment.