Skip to content

BUG: Ensure series/frame mode() keeps int index #38732

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 5 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -202,6 +202,7 @@ Numeric
^^^^^^^
- Bug in :meth:`DataFrame.quantile`, :meth:`DataFrame.sort_values` causing incorrect subsequent indexing behavior (:issue:`38351`)
- Bug in :meth:`DataFrame.select_dtypes` with ``include=np.number`` now retains numeric ``ExtensionDtype`` columns (:issue:`35340`)
- Bug in :meth:`DataFrame.mode` and :meth:`Series.mode` not keeping consistent integer :class:`Index` for empty input (:issue:`33321`)

Conversion
^^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def mode(values, dropna: bool = True) -> Series:
-------
mode : Series
"""
from pandas import Series
from pandas import Index, Series

values = _ensure_arraylike(values)
original = values
Expand All @@ -954,7 +954,9 @@ def mode(values, dropna: bool = True) -> Series:
warn(f"Unable to sort modes: {err}")

result = _reconstruct_data(result, original.dtype, original)
return Series(result)
# Ensure index is type stable (should always use int index)
index = None if len(result) else Index([], dtype="int64")
return Series(result, index=index)


def rank(
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9241,7 +9241,12 @@ def mode(
def f(s):
return s.mode(dropna=dropna)

return data.apply(f, axis=axis)
data = data.apply(f, axis=axis)
# Ensure index is type stable (should always use int index)
if data.empty:
data.index = Index([], dtype="int64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right for 32-bit machines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to int, thanks for catching that


return data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty result case doesn't hit algorithms.mode, so those changes aren't enough to ensure correct dtype


def quantile(
self,
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/methods/test_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pandas import DataFrame, Index
import pandas._testing as tm


def test_empty_df_mode():
df = DataFrame([], columns=["a", "b"])
result = df.mode()
expected = DataFrame([], columns=["a", "b"], index=Index([], dtype="int64"))
tm.assert_frame_equal(result, expected)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find any DataFrame.mode testing - guessing it relies on correctness from Series.mode() testing (in pandas/tests/test_algos.py::TestMode). However, empty result case doesn't hit Series.mode, so some DataFrame testing seems necessary. Is there a better place for this test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We keep them in pandas/tests/frame/test_reductions.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks! Sorry for missing that

2 changes: 1 addition & 1 deletion pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ def test_int64_add_overflow():

class TestMode:
def test_no_mode(self):
exp = Series([], dtype=np.float64)
exp = Series([], dtype=np.float64, index=Index([], dtype="int64"))
tm.assert_series_equal(algos.mode([]), exp)

def test_mode_single(self):
Expand Down