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 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
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 @@ -208,6 +208,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
4 changes: 3 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ def mode(values, dropna: bool = True) -> Series:
mode : Series
"""
from pandas import Series
import pandas.core.indexes.base as ibase

values = _ensure_arraylike(values)
original = values
Expand All @@ -954,7 +955,8 @@ 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)
return Series(result, index=ibase.default_index(len(result)))


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 @@ -9230,7 +9230,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 = ibase.default_index(0)

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
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ def test_mode_sortwarning(self):

tm.assert_frame_equal(result, expected)

def test_mode_empty_df(self):
df = DataFrame([], columns=["a", "b"])
result = df.mode()
expected = DataFrame([], columns=["a", "b"], index=Index([], dtype=int))
tm.assert_frame_equal(result, expected)

def test_operators_timedelta64(self):
df = DataFrame(
{
Expand Down
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=int))
tm.assert_series_equal(algos.mode([]), exp)

def test_mode_single(self):
Expand Down