Skip to content

BUG: idxmax/min (and argmax/min) for Series with underlying ExtensionArray #37924

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

Merged
merged 24 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c000668
fix idxmax/min for Series with underlying 'Int' datatype
tonyyyyip Nov 17, 2020
1632081
test added
tonyyyyip Nov 18, 2020
8018586
editted test
tonyyyyip Nov 18, 2020
b076d23
added test for argmax argmin
tonyyyyip Nov 19, 2020
664e4ec
added validations
tonyyyyip Nov 20, 2020
8403c38
The overhaul
tonyyyyip Nov 20, 2020
741c97a
2nd attempt
tonyyyyip Nov 24, 2020
131ae83
2nd attempt
tonyyyyip Nov 24, 2020
3269fb5
simplified idxmaxmin and added parameterised tests
tonyyyyip Nov 25, 2020
0cfb621
fixed newbie mistake
tonyyyyip Nov 26, 2020
d4b13ac
fixed newbie mistake
tonyyyyip Nov 26, 2020
5648eb9
used any_numeric_dtype in test
tonyyyyip Nov 26, 2020
9a1b332
does this solve the pre-commit check failure now?
tonyyyyip Nov 26, 2020
9f5e683
moved EA's skipna logic from Series.argmin/max to EA.argmin/max
tonyyyyip Nov 26, 2020
e73a3d1
moved EA's skipna logic back to Series
tonyyyyip Nov 27, 2020
d78e28c
moved EA's skipna logic back to Series
tonyyyyip Nov 27, 2020
66f3187
added whatsnew entry and extra tests
tonyyyyip Dec 5, 2020
6f01069
moved tests to tests/reductions/rest_reductions.py
tonyyyyip Dec 5, 2020
3540797
added 1.3 whatsnew entry
tonyyyyip Dec 28, 2020
6d0b68e
moved and restructured tests
tonyyyyip Dec 30, 2020
da5bf06
Merge branch 'master' into fix-argmax
tonyyyyip Dec 30, 2020
4f6d111
moved tests to pandas/tests/extensions/methods.py
tonyyyyip Dec 31, 2020
aa909e9
moved tests to pandas/tests/extensions/methods.py
tonyyyyip Dec 31, 2020
dd0ecde
Merge branch 'fix-argmax' of https://github.com/tonyyyyip/pandas into…
tonyyyyip Dec 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
moved tests to tests/reductions/rest_reductions.py
  • Loading branch information
tonyyyyip committed Dec 28, 2020
commit 6f01069af6e86cf409caa2bdb850ab42011537e1
54 changes: 54 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,16 @@ def test_idxmax(self):
result = s.idxmin()
assert result == 1.1

# GH#36566
s = Series("a", dtype="string").value_counts()
assert s.idxmax() == "a"

# GH#32749
from pandas.tests.extension.decimal import DecimalArray, make_data

s = Series(DecimalArray(make_data()[:1]))
assert s.idxmax() == 0

def test_all_any(self):
ts = tm.makeTimeSeries()
bool_series = ts > 0
Expand Down Expand Up @@ -1007,6 +1017,27 @@ def test_idxminmax_with_inf(self):
assert s.idxmax() == 0
np.isnan(s.idxmax(skipna=False))

@pytest.mark.parametrize(
"op_name, skipna, expected",
[
("idxmax", True, "b"),
("idxmin", True, "a"),
("argmax", True, 1),
("argmin", True, 0),
("idxmax", False, np.nan),
("idxmin", False, np.nan),
("argmax", False, -1),
("argmin", False, -1),
],
)
def test_argminmax_idxminmax(self, any_numeric_dtype, op_name, skipna, expected):
s = Series([1, 2, None], index=["a", "b", "c"], dtype=any_numeric_dtype)
result = getattr(s, op_name)(skipna=skipna)
if pd.isna(expected):
assert np.isnan(result)
else:
assert result == expected


class TestDatetime64SeriesReductions:
# Note: the name TestDatetime64SeriesReductions indicates these tests
Expand Down Expand Up @@ -1072,6 +1103,29 @@ def test_min_max_series(self):
assert isinstance(result, Timestamp)
assert result == exp

@pytest.mark.parametrize(
"op_name, skipna, expected",
[
("idxmax", True, "b"),
("idxmin", True, "a"),
("argmax", True, 1),
("argmin", True, 0),
("idxmax", False, np.nan),
("idxmin", False, np.nan),
("argmax", False, -1),
("argmin", False, -1),
],
)
def test_argminmax_idxminmax(self, op_name, skipna, expected):
s = Series(
[datetime(2020, 1, 1), datetime(2020, 1, 2), None], index=["a", "b", "c"]
)
result = getattr(s, op_name)(skipna=skipna)
if pd.isna(expected):
assert np.isnan(result)
else:
assert result == expected


class TestCategoricalSeriesReductions:
# Note: the name TestCategoricalSeriesReductions indicates these tests
Expand Down
60 changes: 0 additions & 60 deletions pandas/tests/series/test_reductions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from datetime import datetime

import numpy as np
import pytest

Expand Down Expand Up @@ -116,61 +114,3 @@ def test_validate_stat_keepdims():
)
with pytest.raises(ValueError, match=msg):
np.sum(ser, keepdims=True)


@pytest.mark.parametrize(
"op_name, skipna, expected",
[
("idxmax", True, "b"),
("idxmin", True, "a"),
("argmax", True, 1),
("argmin", True, 0),
("idxmax", False, np.nan),
("idxmin", False, np.nan),
("argmax", False, -1),
("argmin", False, -1),
],
)
def test_argminmax_idxminmax(any_numeric_dtype, op_name, skipna, expected):
ser = Series([1, 2, None], index=["a", "b", "c"], dtype=any_numeric_dtype)
result = getattr(ser, op_name)(skipna=skipna)
if pd.isna(expected):
assert np.isnan(result)
else:
assert result == expected


@pytest.mark.parametrize(
"op_name, skipna, expected",
[
("idxmax", True, "b"),
("idxmin", True, "a"),
("argmax", True, 1),
("argmin", True, 0),
("idxmax", False, np.nan),
("idxmin", False, np.nan),
("argmax", False, -1),
("argmin", False, -1),
],
)
def test_argminmax_idxminmax_with_datetime64_dtype(op_name, skipna, expected):
ser = Series(
[datetime(2020, 1, 1), datetime(2020, 1, 2), None], index=["a", "b", "c"]
)
result = getattr(ser, op_name)(skipna=skipna)
if pd.isna(expected):
assert np.isnan(result)
else:
assert result == expected


def test_github_issues():
# GH#36566
ser = Series("a", dtype="string").value_counts()
assert ser.idxmax() == "a"

# GH#32749
from pandas.tests.extension.decimal import DecimalArray, make_data

ser = Series(DecimalArray(make_data()[:1]))
assert ser.idxmax() == 0