-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Deprecating Series.argmin and Series.argmax (#16830) #16955
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
Changes from 3 commits
676bb50
578dbd5
4c770b6
0e039de
db08dda
080fb06
e979c60
e3d3581
b2501b2
7ebf9cd
128f8d4
e106a18
426d8eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,12 +439,35 @@ Other API Changes | |
|
||
Deprecations | ||
~~~~~~~~~~~~ | ||
- :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with ``.to_excel()`` (:issue:`10559`). | ||
|
||
- :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with ``.to_excel()`` (:issue:`10559`). | ||
- ``pd.options.html.border`` has been deprecated in favor of ``pd.options.display.html.border`` (:issue:`15793`). | ||
|
||
- :func:`SeriesGroupBy.nth` has deprecated ``True`` in favor of ``'all'`` for its kwarg ``dropna`` (:issue:`11038`). | ||
|
||
Series.argmax and Series.argmin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a ref to this subsection. make the title something like
|
||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
- The behavior of :func:`Series.argmax` has been deprecated in favor of :func:`Series.idxmax` (:issue:`16830`) | ||
- The behavior of :func:`Series.argmin` has been deprecated in favor of :func:`Series.idxmin` (:issue:`16830`) | ||
|
||
For compatibility with NumPy arrays, ``pd.Series`` implements ``argmax`` and | ||
``argmin``. Since pandas 0.13 :func:`Series.argmax` and | ||
:func:`Series.argmin` returned the *label* of the maximum and minimum, | ||
rather than the *position*. | ||
|
||
Since returning the positional ``argmax`` and ``argmin`` can be useful, | ||
we've deprecated the current behavior of :func:`Series.argmax` and | ||
:func:`Series.argmin`. Using either of these will emit a ``FutureWarning``. | ||
|
||
If you were using ``Series.argmin`` and ``Series.argmax``, please switch to using | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove these last 2 paragraphs. keep it short and sweet. |
||
``idxmin`` and ``idxmax``. In the future, ``Series.argmin`` will return the | ||
*position* of the minimum. Likewise for ``Series.argmax``. | ||
|
||
Until this functionality is implemented in the future, we recommend using | ||
``Series.values.argmin()`` and ``Series.values.argmax()`` for returning | ||
the positional ``argmin`` and ``argmax``. | ||
|
||
.. _whatsnew_0210.prior_deprecations: | ||
|
||
Removal of prior version deprecations/changes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1242,16 +1242,32 @@ def test_idxmin(self): | |
result = s.idxmin() | ||
assert result == 1 | ||
|
||
def test_numpy_argmin(self): | ||
# argmin is aliased to idxmin | ||
data = np.random.randint(0, 11, size=10) | ||
result = np.argmin(Series(data)) | ||
assert result == np.argmin(data) | ||
def test_numpy_argmin_deprecated(self): | ||
# See gh-16830 | ||
data = np.arange(10) | ||
|
||
s = Series(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was like this before, but it would actually be good to use eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you changed it correctly. It's the index of the Series that should be changed, not the actual |
||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the issue deprecation issue as a comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this as a separate function (these deprecation tests) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since all functions in this test will cause the deprecation warning, if I break them into a separate test, what should be left in here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nothing much, maybe even rename the test a bit to reflect what you are testing test_numpy_argmin_deprecated or somesuch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gfyoung I thought that was what I was doing by adding the comments stating that the deprecation warning was also occurring in np.argmax. Could you clarify where you would like a comment added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What he means is just reference the issue number beneath the function definition e.g. "see gh-16830" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh gotcha, thanks for the clarification. I will add that in the next commit. |
||
# The deprecation of Series.argmin also causes a deprecation | ||
# warning when calling np.argmin. This behavior is temporary | ||
# until the implemention of Series.argmin is corrected. | ||
result = np.argmin(s) | ||
|
||
assert result == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you asserting here directly to 0, and below to the result of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just changed them all to compare directly to scalars. I think that's preferable. |
||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
# argmin is aliased to idxmin | ||
result = s.argmin() | ||
|
||
expected = s.idxmin() | ||
assert result == expected | ||
|
||
if not _np_version_under1p10: | ||
msg = "the 'out' parameter is not supported" | ||
tm.assert_raises_regex(ValueError, msg, np.argmin, | ||
Series(data), out=data) | ||
with tm.assert_produces_warning(FutureWarning, | ||
check_stacklevel=False): | ||
msg = "the 'out' parameter is not supported" | ||
tm.assert_raises_regex(ValueError, msg, np.argmin, | ||
s, out=data) | ||
|
||
def test_idxmax(self): | ||
# test idxmax | ||
|
@@ -1297,17 +1313,32 @@ def test_idxmax(self): | |
result = s.idxmin() | ||
assert result == 1.1 | ||
|
||
def test_numpy_argmax(self): | ||
|
||
# argmax is aliased to idxmax | ||
def test_numpy_argmax_deprecated(self): | ||
# See gh-16830 | ||
data = np.random.randint(0, 11, size=10) | ||
result = np.argmax(Series(data)) | ||
assert result == np.argmax(data) | ||
data = np.arange(10) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
s = Series(data) | ||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): | ||
# The deprecation of Series.argmax also causes a deprecation | ||
# warning when calling np.argmax. This behavior is temporary | ||
# until the implemention of Series.argmax is corrected. | ||
result = np.argmax(s) | ||
assert result == 9 | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
# argmax is aliased to idxmax | ||
result = s.argmax() | ||
|
||
expected = s.idxmax() | ||
assert result == expected | ||
|
||
if not _np_version_under1p10: | ||
msg = "the 'out' parameter is not supported" | ||
tm.assert_raises_regex(ValueError, msg, np.argmax, | ||
Series(data), out=data) | ||
with tm.assert_produces_warning(FutureWarning, | ||
check_stacklevel=False): | ||
msg = "the 'out' parameter is not supported" | ||
tm.assert_raises_regex(ValueError, msg, np.argmax, | ||
s, out=data) | ||
|
||
def test_ptp(self): | ||
N = 1000 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# pylint: disable-msg=E1101,W0612 | ||
|
||
import operator | ||
import warnings | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
@@ -1379,11 +1380,24 @@ def test_numpy_func_call(self): | |
# numpy passes in 'axis=None' or `axis=-1' | ||
funcs = ['sum', 'cumsum', 'var', 'mean', | ||
'prod', 'cumprod', 'std', 'argsort', | ||
'argmin', 'argmax', 'min', 'max'] | ||
'min', 'max'] | ||
for func in funcs: | ||
for series in ('bseries', 'zbseries'): | ||
getattr(np, func)(getattr(self, series)) | ||
|
||
def test_deprecated_numpy_func_call(self): | ||
# NOTE: These should be add to the 'test_numpy_func_call' test above | ||
# once the behavior of argmin/argmax is corrected. | ||
funcs = ['argmin', 'argmax'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment that we need to include them again in the test above when behaviour of argmin/argmax is changes? (to make sure we don't just delete the test when the deprecation is removed) |
||
for func in funcs: | ||
for series in ('bseries', 'zbseries'): | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore") | ||
getattr(np, func)(getattr(self, series)) | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
getattr(getattr(self, series), func)() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'datetime_type', (np.datetime64, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be in a separate sub-section