Skip to content

Commit a6b0387

Browse files
DGradyDaniel Grady
authored and
Daniel Grady
committed
Test expected behaviors for idxmin, idxmax on Series
1 parent 757a3b4 commit a6b0387

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pandas/tests/groupby/test_groupby.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,8 @@ def test_non_cython_api(self):
23392339
assert_frame_equal(result, expected)
23402340

23412341
# idxmax
2342-
expected = DataFrame([[0.0, 0.0], [nan, 2.0]], columns=['B', 'C'], index=[1, 3])
2342+
expected = DataFrame([[0.0, 0.0], [nan, 2.0]], columns=['B', 'C'],
2343+
index=[1, 3])
23432344
expected.index.name = 'A'
23442345
result = g.idxmax()
23452346
assert_frame_equal(result, expected)

pandas/tests/series/test_operators.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,13 +1859,57 @@ def test_op_duplicate_index(self):
18591859
assert_series_equal(result, expected)
18601860

18611861
def test_argminmax(self):
1862-
# GH13595
1863-
1864-
# Expected behavior for arg min/max in the presence of NA and Inf
1862+
# Series.argmin, Series.argmax are aliased to Series.idxmin,
1863+
# Series.idxmax
1864+
1865+
# Expected behavior for empty Series
1866+
s = pd.Series([])
1867+
1868+
with pytest.raises(ValueError):
1869+
s.argmin()
1870+
with pytest.raises(ValueError):
1871+
s.argmin(skipna=False)
1872+
with pytest.raises(ValueError):
1873+
s.argmax()
1874+
with pytest.raises(ValueError):
1875+
s.argmax(skipna=False)
1876+
1877+
# For numeric data with NA and Inf (GH #13595)
18651878
s = pd.Series([0, -np.inf, np.inf, np.nan])
18661879

18671880
assert s.argmin() == 1
18681881
assert np.isnan(s.argmin(skipna=False))
18691882

18701883
assert s.argmax() == 2
18711884
assert np.isnan(s.argmax(skipna=False))
1885+
1886+
# Using old-style behavior that treat floating point nan, -inf, and
1887+
# +inf as missing
1888+
s = pd.Series([0, -np.inf, np.inf, np.nan])
1889+
1890+
with pd.option_context('mode.use_inf_as_null', True):
1891+
assert s.argmin() == 0
1892+
assert np.isnan(s.argmin(skipna=False))
1893+
assert s.argmax() == 0
1894+
np.isnan(s.argmax(skipna=False))
1895+
1896+
# For non-NA strings
1897+
s = pd.Series(['foo', 'foo', 'bar', 'bar', 'baz'])
1898+
1899+
assert s.argmin() == 2
1900+
assert s.argmin(skipna=False) == 2
1901+
1902+
assert s.argmax() == 0
1903+
assert s.argmax(skipna=False) == 0
1904+
1905+
# For mixed string and NA
1906+
s = pd.Series(['foo', 'foo', 'bar', 'bar', None, np.nan, 'baz'])
1907+
1908+
with pytest.raises(TypeError):
1909+
s.argmin()
1910+
with pytest.raises(TypeError):
1911+
s.argmin(skipna=False)
1912+
with pytest.raises(TypeError):
1913+
s.argmax()
1914+
with pytest.raises(TypeError):
1915+
s.argmax(skipna=False)

0 commit comments

Comments
 (0)