-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Return mode even if single value (#15714) #15744
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
Changes from 4 commits
44dbbb2
26db131
0e2dec0
5f829e1
5f36395
8c08cd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -809,18 +809,18 @@ def test_mode(self): | |
"E": [8, 8, 1, 1, 3, 3]}) | ||
tm.assert_frame_equal(df[["A"]].mode(), | ||
pd.DataFrame({"A": [12]})) | ||
expected = pd.Series([], dtype='int64', name='D').to_frame() | ||
expected = pd.Series([0, 1, 2, 3, 4, 5], dtype='int64', name='D').to_frame() | ||
tm.assert_frame_equal(df[["D"]].mode(), expected) | ||
expected = pd.Series([1, 3, 8], dtype='int64', name='E').to_frame() | ||
tm.assert_frame_equal(df[["E"]].mode(), expected) | ||
tm.assert_frame_equal(df[["A", "B"]].mode(), | ||
pd.DataFrame({"A": [12], "B": [10.]})) | ||
tm.assert_frame_equal(df.mode(), | ||
pd.DataFrame({"A": [12, np.nan, np.nan], | ||
"B": [10, np.nan, np.nan], | ||
"C": [8, 9, np.nan], | ||
"D": [np.nan, np.nan, np.nan], | ||
"E": [1, 3, 8]})) | ||
pd.DataFrame({"A": [12, np.nan, np.nan, np.nan, np.nan, np.nan], | ||
"B": [10, np.nan, np.nan, np.nan, np.nan, np.nan], | ||
"C": [8, 9, np.nan, np.nan, np.nan, np.nan], | ||
"D": [0, 1, 2, 3, 4, 5], | ||
"E": [1, 3, 8, np.nan, np.nan, np.nan]})) | ||
|
||
# outputs in sorted order | ||
df["C"] = list(reversed(df["C"])) | ||
|
@@ -837,20 +837,9 @@ def test_mode(self): | |
df = pd.DataFrame({"A": np.arange(6, dtype='int64'), | ||
"B": pd.date_range('2011', periods=6), | ||
"C": list('abcdef')}) | ||
exp = pd.DataFrame({"A": pd.Series([], dtype=df["A"].dtype), | ||
"B": pd.Series([], dtype=df["B"].dtype), | ||
"C": pd.Series([], dtype=df["C"].dtype)}) | ||
tm.assert_frame_equal(df.mode(), exp) | ||
|
||
# and also when not empty | ||
df.loc[1, "A"] = 0 | ||
df.loc[4, "B"] = df.loc[3, "B"] | ||
df.loc[5, "C"] = 'e' | ||
exp = pd.DataFrame({"A": pd.Series([0], dtype=df["A"].dtype), | ||
"B": pd.Series([df.loc[3, "B"]], | ||
dtype=df["B"].dtype), | ||
"C": pd.Series(['e'], dtype=df["C"].dtype)}) | ||
|
||
exp = pd.DataFrame({"A": pd.Series(np.arange(6, dtype='int64'), dtype=df["A"].dtype), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed all but the ones in the |
||
"B": pd.Series(pd.date_range('2011', periods=6), dtype=df["B"].dtype), | ||
"C": pd.Series(list('abcdef'), dtype=df["C"].dtype)}) | ||
tm.assert_frame_equal(df.mode(), exp) | ||
|
||
def test_operators_timedelta64(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1256,12 +1256,30 @@ def test_no_mode(self): | |
exp = Series([], dtype=np.float64) | ||
tm.assert_series_equal(algos.mode([]), exp) | ||
|
||
exp = Series([], dtype=np.int) | ||
def test_mode_single(self): | ||
exp_single = [1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the issue number as a comment |
||
data_single = [1] | ||
|
||
for dt in np.typecodes['AllInteger'] + np.typecodes['Float']: | ||
s = Series(data_single, dtype=dt) | ||
exp = Series(exp_single, dtype=dt) | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the result for a double-but-same value e.g.
|
||
exp = Series([1], dtype=np.int) | ||
tm.assert_series_equal(algos.mode([1]), exp) | ||
|
||
exp = Series([], dtype=np.object) | ||
exp = Series(['a', 'b', 'c'], dtype=np.object) | ||
tm.assert_series_equal(algos.mode(['a', 'b', 'c']), exp) | ||
|
||
def test_mode_single(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks duplicated |
||
exp_single = [1] | ||
data_single = [1] | ||
|
||
for dt in np.typecodes['AllInteger'] + np.typecodes['Float']: | ||
s = Series(data_single, dtype=dt) | ||
exp = Series(exp_single, dtype=dt) | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
def test_number_mode(self): | ||
exp_single = [1] | ||
data_single = [1] * 5 + [2] * 3 | ||
|
@@ -1295,7 +1313,8 @@ def test_strobj_mode(self): | |
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
def test_datelike_mode(self): | ||
exp = Series([], dtype="M8[ns]") | ||
exp = Series(['1900-05-03', '2011-01-03', | ||
'2013-01-02'], dtype="M8[ns]") | ||
s = Series(['2011-01-03', '2013-01-02', | ||
'1900-05-03'], dtype='M8[ns]') | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
@@ -1306,7 +1325,8 @@ def test_datelike_mode(self): | |
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
def test_timedelta_mode(self): | ||
exp = Series([], dtype='timedelta64[ns]') | ||
exp = Series(['-1 days', '0 days', '1 days'], | ||
dtype='timedelta64[ns]') | ||
s = Series(['1 days', '-1 days', '0 days'], | ||
dtype='timedelta64[ns]') | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
@@ -1326,13 +1346,13 @@ def test_uint64_overflow(self): | |
s = Series([1, 2**63, 2**63], dtype=np.uint64) | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
exp = Series([], dtype=np.uint64) | ||
exp = Series([1, 2**63], dtype=np.uint64) | ||
s = Series([1, 2**63], dtype=np.uint64) | ||
tm.assert_series_equal(algos.mode(s), exp) | ||
|
||
def test_categorical(self): | ||
c = Categorical([1, 2]) | ||
exp = Series([], dtype=np.int64) | ||
exp = Series([1, 2], dtype=np.int64) | ||
tm.assert_series_equal(algos.mode(c), exp) | ||
|
||
c = Categorical([1, 'a', 'a']) | ||
|
@@ -1345,7 +1365,7 @@ def test_categorical(self): | |
|
||
def test_index(self): | ||
idx = Index([1, 2, 3]) | ||
exp = Series([], dtype=np.int64) | ||
exp = Series([1, 2, 3], dtype=np.int64) | ||
tm.assert_series_equal(algos.mode(idx), exp) | ||
|
||
idx = Index([1, 'a', 'a']) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this (or followup), we should make this a shared doc-string :>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback — not sure how to do this, I reckon this is to reference the same-ish doc string in all relevant places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you woould move
def mode
togeneric.py
(leaving a stub here and in series for an@Appender
doc-string (look at virtually any other doc-string, e.g.fillna
)can do this as a follow up though (or here, up 2 u)