Skip to content

Commit 0725916

Browse files
committed
DOC, TST: Add more tests for mode()
Updates documentation to describe the "values" parameter in the signature. Adds tests for properly identifying the mode in a np.uint64 array. The algorithm currently will convert uint64 to int64, but that is not an issue since all elements in the uint64 range are unique in int64 as well. In addition, the result is ultimately cast to the correct type at the end.
1 parent dfa7c1b commit 0725916

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pandas/core/algorithms.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,19 @@ def duplicated(values, keep='first'):
540540

541541

542542
def mode(values):
543-
"""Returns the mode or mode(s) of the passed Series or ndarray (sorted)"""
543+
"""
544+
Returns the mode(s) of an array.
545+
546+
Parameters
547+
----------
548+
values : array-like
549+
Array over which to check for duplicate values.
550+
551+
Returns
552+
-------
553+
mode : Series
554+
"""
555+
544556
# must sort because hash order isn't necessarily defined.
545557
from pandas.core.series import Series
546558

@@ -553,6 +565,9 @@ def mode(values):
553565

554566
dtype = values.dtype
555567
if is_integer_dtype(values):
568+
# This also works if dtype is uint64 because there is a 1-1
569+
# correspondence between int64 and uint64, and we will cast
570+
# all elements back to their correct uint64 values.
556571
values = _ensure_int64(values)
557572
result = constructor(sorted(htable.mode_int64(values)), dtype=dtype)
558573

pandas/tests/test_algos.py

+14
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,20 @@ def test_int64_add_overflow():
12111211
b_mask=np.array([False, True]))
12121212

12131213

1214+
def test_mode_uint64():
1215+
s = Series([1, 2**63, 2**63], dtype=np.uint64)
1216+
exp = Series([2**63], dtype=np.uint64)
1217+
1218+
tm.assert_series_equal(algos.mode(s), exp)
1219+
tm.assert_series_equal(s.mode(), exp)
1220+
1221+
s = Series([1, 2**63], dtype=np.uint64)
1222+
exp = Series([], dtype=np.uint64)
1223+
1224+
tm.assert_series_equal(algos.mode(s), exp)
1225+
tm.assert_series_equal(s.mode(), exp)
1226+
1227+
12141228
if __name__ == '__main__':
12151229
import nose
12161230
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)