Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added deprecate_nonkeyword_arguments to function where #41523

Merged
merged 26 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
110d63f
added deprecate_nonkeyword_arguments to function where
May 17, 2021
6d78fb6
added test
May 18, 2021
4edab7d
Merge branch 'pandas-dev:master' into shiny-new-feature
Jiezheng2018 May 18, 2021
6692733
updated generic.py where function
May 18, 2021
861e2bb
added line 651 in whatsnew v1.3.0.rst
May 18, 2021
b4050f4
removed cond from test_where
May 18, 2021
518d204
changed test to be s.where(s > 1, 10, False) to triger warning
May 18, 2021
eaed956
Merge branch 'master' into shiny-new-feature
Jiezheng2018 May 19, 2021
a5ca29b
changed version to None
May 20, 2021
3d58b4b
deleted test_where from test_generic.py
May 20, 2021
90b1fde
added test_where to frame folder
May 20, 2021
72a9983
added test_where to frame folder
May 20, 2021
65bba0b
added test_where to series folder
May 20, 2021
e1e32bc
resolve conflicts
May 24, 2021
0258119
edited the whatsnew, mentioned other than cond and other
May 24, 2021
40df58b
changed stacklevel to 3 for futurewarning
May 24, 2021
3ea8e92
changed frame/index/test_where_non_keyword
May 24, 2021
79ffbf7
added where function in frame.py
May 24, 2021
cfdb43a
added where function in series.py
May 24, 2021
fe7d729
changed allowed_args in frame
May 24, 2021
c40cb38
changed allowed_args in series
May 24, 2021
8e875ac
changed allowed_args in generic
May 24, 2021
9c102ff
adding github issue number
May 24, 2021
74c0393
fixup
MarcoGorelli May 25, 2021
17e892a
revert change to mask
MarcoGorelli May 25, 2021
5aad6bc
fixup gh issue number
MarcoGorelli May 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -681,6 +681,7 @@ Deprecations
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (: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 @@ -10688,6 +10688,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 @@ -5310,6 +5310,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