Skip to content

Commit

Permalink
BUG: Ensure series/frame mode() keeps int index (pandas-dev#38732)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeitlin11 authored and luckyvs1 committed Jan 20, 2021
1 parent 9bdd9fc commit 277d941
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
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 @@ -210,6 +210,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

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

0 comments on commit 277d941

Please sign in to comment.