8000 ENH: Implement mode(dropna=False) by reidy-p · Pull Request #20779 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Implement mode(dropna=False) #20779

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 8 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test for sort warning in DataFrame and clean up sort test for Series
  • Loading branch information
reidy-p committed May 29, 2018
commit 0399822f16bbe44cfb910ab342dea7774d8a0d4e
67 changes: 39 additions & 28 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,44 +858,55 @@ def wrapper(x):
(True, {'H': [8, 9, np.nan, np.nan],
'I': [8, 9, np.nan, np.nan],
'J': [1, np.nan, np.nan, np.nan],
'K': ['a', np.nan, np.nan, np.nan],
'L': Categorical(['a', np.nan, np.nan, np.nan],
'K': Categorical(['a', np.nan, np.nan, np.nan],
categories=['a']),
'M': to_datetime(['2000-1-2', 'NaT', 'NaT', 'NaT']),
'N': to_timedelta(['1 days', 'nan', 'nan', 'nan']),
'O': [0, 1, 2, 3]}),
'L': to_datetime(['2000-1-2', 'NaT', 'NaT', 'NaT']),
'M': to_timedelta(['1 days', 'nan', 'nan', 'nan']),
'N': [0, 1, 2, 3]}),
(False, {'H': [8, 9, np.nan, np.nan],
'I': [8, 9, np.nan, np.nan],
'J': [1, np.nan, np.nan, np.nan],
'K': [np.nan, 'a', np.nan, np.nan],
'L': Categorical([np.nan, 'a', np.nan, np.nan],
'K': Categorical([np.nan, 'a', np.nan, np.nan],
categories=['a']),
'M': to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']),
'N': to_timedelta(['nan', '1 days', 'nan', 'nan']),
'O': [0, 1, 2, 3]})
'L': to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']),
'M': to_timedelta(['nan', '1 days', 'nan', 'nan']),
'N': [0, 1, 2, 3]})
])
def test_mode_dropna(self, dropna, expected):

df = pd.DataFrame({"A": [12, 12, 19, 11],
"B": [10, 10, np.nan, 3],
"C": [1, np.nan, np.nan, np.nan],
"D": [np.nan, np.nan, 'a', np.nan],
"E": Categorical([np.nan, np.nan, 'a', np.nan]),
"F": to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']),
"G": to_timedelta(['1 days', 'nan', 'nan', 'nan']),
"H": [8, 8, 9, 9],
"I": [9, 9, 8, 8],
"J": [1, 1, np.nan, np.nan],
"K": [np.nan, np.nan, 'a', 'a'],
"L": Categorical(['a', np.nan, 'a', np.nan]),
"M": to_datetime(['2000-1-2', '2000-1-2',
'NaT', 'NaT']),
"N": to_timedelta(['1 days', 'nan',
'1 days', 'nan']),
"O": np.arange(4, dtype='int64')})
df = DataFrame({"A": [12, 12, 19, 11],
"B": [10, 10, np.nan, 3],
"C": [1, np.nan, np.nan, np.nan],
"D": [np.nan, np.nan, 'a', np.nan],
"E": Categorical([np.nan, np.nan, 'a', np.nan]),
"F": to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']),
"G": to_timedelta(['1 days', 'nan', 'nan', 'nan']),
"H": [8, 8, 9, 9],
"I": [9, 9, 8, 8],
"J": [1, 1, np.nan, np.nan],
"K": Categorical(['a', np.nan, 'a', np.nan]),
"L": to_datetime(['2000-1-2', '2000-1-2',
'NaT', 'NaT']),
"M": to_timedelta(['1 days', 'nan',
'1 days', 'nan']),
"N": np.arange(4, dtype='int64')})

result = df[sorted(list(expected.keys()))].mode(dropna=dropna)
8000 expected = pd.DataFrame(expected)
expected = DataFrame(expected)
tm.assert_frame_equal(result, expected)

@pytest.mark.skipif(not compat.PY3, reason="only PY3")
def test_mode_sortwarning(self):
# Check for the warning that is raised when the mode
# results cannot be sorted

df = DataFrame({"A": [np.nan, np.nan, 'a', 'a']})
expected = DataFrame({'A': ['a', np.nan]})

with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
result = df.mode(dropna=False)
result = result.sort_values(by='A').reset_index(drop=True)

tm.assert_frame_equal(result, expected)

def test_operators_timedelta64(self):
Expand Down
10 changes: 4 additions & 6 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,16 @@ def test_mode_intoverflow(self, dropna, expected1, expected2):
tm.assert_series_equal(s.mode(dropna), expected2)

@pytest.mark.skipif(not compat.PY3, reason="only PY3")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that in Python 3 a warning is raised if the resulting modes cannot be sorted but there is no warning in Python 2:

Python 3

In [2]: s = pd.Series([1, 'foo', 'foo', np.nan, np.nan])                       
In [3]: s.mode(False).sort_values().reset_index(drop=True)
Out[3]:
/home/paul/pandas-reidy-p/pandas/core/algorithms.py:840: UserWarning: Unable                                    
 to sort modes: '<' not supported between instances of 'float' and 'str'  
  warn("Unable to sort modes: {error}".format(error=e))                                                             
0    foo
1    NaN
dtype: object  

Python 2

In [2]: s = pd.Series([1, 'foo', 'foo', np.nan, np.nan])                       
In [3]: s.mode(False).sort_values().reset_index(drop=True)
Out[3]:
0    foo
1    NaN
dtype: object  

Relevant source code:

f = getattr(htable, "mode_{dtype}".format(dtype=ndtype))
result = f(values)
try:
result = np.sort(result)
except TypeError as e:
warn("Unable to sort modes: {error}".format(error=e))

@pytest.mark.parametrize('dropna, expected', [
(False, ['foo', np.nan]),
])
def test_mode_sortwarning(self, dropna, expected):
def test_mode_sortwarning(self):
# Check for the warning that is raised when the mode
# results cannot be sorted

expected = Series(expected)
expected = Series(['foo', np.nan])
s = Series([1, 'foo', 'foo', np.nan, np.nan])

with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
result = s.mode(dropna).sort_values().reset_index(drop=True)
result = s.mode(dropna=False)
result = result.sort_values().reset_index(drop=True)

tm.assert_series_equal(result, expected)

Expand Down
0