Skip to content

Commit 4c1720d

Browse files
author
Roger Thomas
committed
Update Error Messages
1 parent 333e126 commit 4c1720d

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

pandas/core/algorithms.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,14 @@ def select_n_slow(dropped, n, keep, method):
931931
_select_methods = {'nsmallest': nsmallest, 'nlargest': nlargest}
932932

933933

934+
def _is_valid_dtype_n_method(dtype):
935+
"""Helper function to determine if `dtpye` is valid for
936+
nsmallest/nlargest methods
937+
"""
938+
return ((is_numeric_dtype(dtype) and not is_complex_dtype(dtype)) or
939+
needs_i8_conversion(dtype))
940+
941+
934942
def select_n_series(series, n, keep, method):
935943
"""Implement n largest/smallest for pandas Series
936944
@@ -946,8 +954,7 @@ def select_n_series(series, n, keep, method):
946954
nordered : Series
947955
"""
948956
dtype = series.dtype
949-
if not ((is_numeric_dtype(dtype) and not is_complex_dtype(dtype)) or
950-
needs_i8_conversion(dtype)):
957+
if not _is_valid_dtype_n_method(dtype):
951958
raise TypeError("Cannot use method '{method}' with "
952959
"dtype {dtype}".format(method=method, dtype=dtype))
953960

@@ -985,6 +992,13 @@ def select_n_frame(frame, columns, n, method, keep):
985992
if not is_list_like(columns):
986993
columns = [columns]
987994
columns = list(columns)
995+
for column in columns:
996+
dtype = frame[column].dtype
997+
if not _is_valid_dtype_n_method(dtype):
998+
raise TypeError((
999+
"Column {column!r} has dtype {dtype}, cannot use method "
1000+
"{method!r} with this dtype"
1001+
).format(column=column, dtype=dtype, method=method))
9881002

9891003
def get_indexer(current_indexer, other_indexer):
9901004
"""Helper function to concat `current_indexer` and `other_indexer`

pandas/tests/frame/test_analytics.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,9 @@ def df_main_dtypes():
19381938

19391939
class TestNLargestNSmallest(object):
19401940

1941+
dtype_error_msg_template = ("Column {column!r} has dtype {dtype}, cannot "
1942+
"use method {method!r} with this dtype")
1943+
19411944
# ----------------------------------------------------------------------
19421945
# Top / bottom
19431946
@pytest.mark.parametrize(
@@ -1962,11 +1965,10 @@ class TestNLargestNSmallest(object):
19621965
def test_n(self, df_strings, method, n, order):
19631966
# GH10393
19641967
df = df_strings
1965-
if order[0] == 'b':
1968+
if 'b' in order:
19661969

1967-
# Only expect error when 'b' is first in order, as 'a' and 'c' are
1968-
# unique
1969-
error_msg = "Cannot use method '%s' with dtype object" % method
1970+
error_msg = self.dtype_error_msg_template.format(
1971+
column='b', method=method, dtype='object')
19701972
with tm.assertRaisesRegexp(TypeError, error_msg):
19711973
getattr(df, method)(n, order)
19721974
else:
@@ -1982,18 +1984,15 @@ def test_n(self, df_strings, method, n, order):
19821984
))
19831985
def test_n_error(self, df_main_dtypes, method, columns):
19841986
df = df_main_dtypes
1985-
msg = "Cannot use method '%s' with dtype %s" % (
1986-
method, df[columns[1]].dtype
1987-
)
1988-
with tm.assertRaisesRegexp(TypeError, msg):
1987+
error_msg = self.dtype_error_msg_template.format(
1988+
column=columns[1], method=method, dtype=df[columns[1]].dtype)
1989+
with tm.assertRaisesRegexp(TypeError, error_msg):
19891990
getattr(df, method)(2, columns)
19901991

19911992
def test_n_all_dtypes(self, df_main_dtypes):
19921993
df = df_main_dtypes
19931994
df.nsmallest(2, list(set(df) - {'category_string', 'string'}))
1994-
df.nsmallest(2, ['int', 'string']) # int column is unique => OK
19951995
df.nlargest(2, list(set(df) - {'category_string', 'string'}))
1996-
df.nlargest(2, ['int', 'string']) # int column is unique => OK
19971996

19981997
def test_n_identical_values(self):
19991998
# GH15297

0 commit comments

Comments
 (0)