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

Fix idxmax() / idxmin() for Series work properly #1078

Merged
merged 8 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 39 additions & 7 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,7 @@ def idxmax(self, skipna=True):
"""
Return the row label of the maximum value.

If multiple values equal the maximum, the row label with that
If multiple values equal the maximum, the first row label with that
value is returned.

Parameters
Expand Down Expand Up @@ -3158,16 +3158,32 @@ def idxmax(self, skipna=True):

>>> s.idxmax()
('b', 'f')

If multiple values equal the maximum, the first row label with that
value is returned.

>>> s = ks.Series([1, 100, 1, 100, 1, 100], index=[10, 3, 5, 2, 1, 8])
>>> s
10 1
3 100
5 1
2 100
1 1
8 100
Name: 0, dtype: int64

>>> s.idxmax()
3
"""
sdf = self._internal._sdf
scol = self._scol
index_scols = self._internal.index_scols
# desc_nulls_(last|first) is used via Py4J directly because
# it's not supported in Spark 2.3.
if skipna:
sdf = sdf.orderBy(Column(scol._jc.desc_nulls_last()))
sdf = sdf.orderBy(Column(scol._jc.desc_nulls_last()), F.monotonically_increasing_id())
else:
sdf = sdf.orderBy(Column(scol._jc.desc_nulls_first()))
sdf = sdf.orderBy(Column(scol._jc.desc_nulls_first()), F.monotonically_increasing_id())
results = sdf.select([scol] + index_scols).take(1)
if len(results) == 0:
raise ValueError("attempt to get idxmin of an empty sequence")
Expand All @@ -3185,7 +3201,7 @@ def idxmin(self, skipna=True):
"""
Return the row label of the minimum value.

If multiple values equal the minimum, the row label with that
If multiple values equal the minimum, the first row label with that
value is returned.

Parameters
Expand Down Expand Up @@ -3250,16 +3266,32 @@ def idxmin(self, skipna=True):

>>> s.idxmin()
('b', 'f')

If multiple values equal the minimum, the first row label with that
value is returned.

>>> s = ks.Series([1, 100, 1, 100, 1, 100], index=[10, 3, 5, 2, 1, 8])
>>> s
10 1
3 100
5 1
2 100
1 1
8 100
Name: 0, dtype: int64

>>> s.idxmin()
10
"""
sdf = self._internal._sdf
scol = self._scol
index_scols = self._internal.index_scols
# asc_nulls_(list|first)is used via Py4J directly because
# asc_nulls_(last|first)is used via Py4J directly because
# it's not supported in Spark 2.3.
if skipna:
sdf = sdf.orderBy(Column(scol._jc.asc_nulls_last()))
itholic marked this conversation as resolved.
Show resolved Hide resolved
sdf = sdf.orderBy(Column(scol._jc.asc_nulls_last()), F.monotonically_increasing_id())
else:
sdf = sdf.orderBy(Column(scol._jc.asc_nulls_first()))
sdf = sdf.orderBy(Column(scol._jc.asc_nulls_first()), F.monotonically_increasing_id())
results = sdf.select([scol] + index_scols).take(1)
if len(results) == 0:
raise ValueError("attempt to get idxmin of an empty sequence")
Expand Down
12 changes: 12 additions & 0 deletions databricks/koalas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ def test_idxmax(self):
with self.assertRaisesRegex(ValueError, "an empty sequence"):
kser.idxmax()

pser = pd.Series([1, 100, None, 100, 1, 100], index=[10, 3, 5, 2, 1, 8])
kser = ks.Series(pser)

self.assertEqual(kser.idxmax(), pser.idxmax())
self.assertEqual(repr(kser.idxmax(skipna=False)), repr(pser.idxmax(skipna=False)))

def test_idxmin(self):
pser = pd.Series(data=[1, 4, 5], index=['A', 'B', 'C'])
kser = ks.Series(pser)
Expand All @@ -679,6 +685,12 @@ def test_idxmin(self):
with self.assertRaisesRegex(ValueError, "an empty sequence"):
kser.idxmin()

pser = pd.Series([1, 100, None, 100, 1, 100], index=[10, 3, 5, 2, 1, 8])
kser = ks.Series(pser)

self.assertEqual(kser.idxmin(), pser.idxmin())
self.assertEqual(repr(kser.idxmin(skipna=False)), repr(pser.idxmin(skipna=False)))

def test_shift(self):
pser = pd.Series([10, 20, 15, 30, 45], name='x')
kser = ks.Series(pser)
Expand Down