Skip to content

Commit 26db131

Browse files
author
Robin
committed
Return mode even if single value (#15714)
1 parent a73e451 commit 26db131

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

pandas/_libs/hashtable_func_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def mode_{{dtype}}(ndarray[{{ctype}}] values):
233233
def mode_{{dtype}}({{ctype}}[:] values):
234234
{{endif}}
235235
cdef:
236-
int count, max_count = 2
236+
int count, max_count = 1
237237
int j = -1 # so you can do +=
238238
Py_ssize_t k
239239
kh_{{table_type}}_t *table

pandas/core/categorical.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1876,8 +1876,7 @@ def mode(self):
18761876
"""
18771877
Returns the mode(s) of the Categorical.
18781878
1879-
Empty if nothing occurs at least 2 times. Always returns `Categorical`
1880-
even if only one value.
1879+
Always returns `Categorical` even if only one value.
18811880
18821881
Returns
18831882
-------

pandas/core/frame.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5168,9 +5168,8 @@ def _get_agg_axis(self, axis_num):
51685168

51695169
def mode(self, axis=0, numeric_only=False):
51705170
"""
5171-
Gets the mode(s) of each element along the axis selected. Empty if
5172-
nothing has 2+ occurrences. Adds a row for each mode per label, fills
5173-
in gaps with nan.
5171+
Gets the mode(s) of each element along the axis selected. Adds a row
5172+
for each mode per label, fills in gaps with nan.
51745173
51755174
Note that there could be multiple values returned for the selected
51765175
axis (when more than one item share the maximum frequency), which is

pandas/core/series.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,7 @@ def count(self, level=None):
11921192
def mode(self):
11931193
"""Return the mode(s) of the dataset.
11941194
1195-
Empty if nothing occurs at least 2 times. Always returns Series even
1196-
if only one value is returned.
1195+
Always returns Series even if only one value is returned.
11971196
11981197
Returns
11991198
-------

pandas/tests/test_algos.py

+9
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,15 @@ def test_no_mode(self):
12621262
exp = Series([], dtype=np.object)
12631263
tm.assert_series_equal(algos.mode(['a', 'b', 'c']), exp)
12641264

1265+
def test_mode_single(self):
1266+
exp_single = [1]
1267+
data_single = [1]
1268+
1269+
for dt in np.typecodes['AllInteger'] + np.typecodes['Float']:
1270+
s = Series(data_single, dtype=dt)
1271+
exp = Series(exp_single, dtype=dt)
1272+
tm.assert_series_equal(algos.mode(s), exp)
1273+
12651274
def test_number_mode(self):
12661275
exp_single = [1]
12671276
data_single = [1] * 5 + [2] * 3

0 commit comments

Comments
 (0)