Skip to content

Commit 3840771

Browse files
authored
Merge pull request #5 from RogerThomas/add_more_error_tests
Add More Error Tests
2 parents c86ce45 + af493a7 commit 3840771

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

pandas/tests/frame/test_analytics.py

+57-30
Original file line numberDiff line numberDiff line change
@@ -1916,13 +1916,33 @@ def df_strings():
19161916
'c': np.random.permutation(10).astype('float64')})
19171917

19181918

1919+
@pytest.fixture
1920+
def df_main_dtypes():
1921+
return pd.DataFrame(
1922+
{'group': [1, 1, 2],
1923+
'int': [1, 2, 3],
1924+
'float': [4., 5., 6.],
1925+
'string': list('abc'),
1926+
'category_string': pd.Series(list('abc')).astype('category'),
1927+
'category_int': [7, 8, 9],
1928+
'datetime': pd.date_range('20130101', periods=3),
1929+
'datetimetz': pd.date_range('20130101',
1930+
periods=3,
1931+
tz='US/Eastern'),
1932+
'timedelta': pd.timedelta_range('1 s', periods=3, freq='s')},
1933+
columns=['group', 'int', 'float', 'string',
1934+
'category_string', 'category_int',
1935+
'datetime', 'datetimetz',
1936+
'timedelta'])
1937+
1938+
19191939
class TestNLargestNSmallest(object):
19201940

19211941
# ----------------------------------------------------------------------
19221942
# Top / bottom
19231943
@pytest.mark.parametrize(
1924-
'n, order',
1925-
product(range(1, 11),
1944+
'method, n, order',
1945+
product(['nsmallest', 'nlargest'], range(1, 11),
19261946
[['a'],
19271947
['c'],
19281948
['a', 'b'],
@@ -1939,39 +1959,46 @@ class TestNLargestNSmallest(object):
19391959
['b', 'c', 'c'],
19401960
19411961
]))
1942-
def test_n(self, df_strings, n, order):
1962+
def test_n(self, df_strings, method, n, order):
19431963
# GH10393
19441964
df = df_strings
1945-
1946-
error_msg = (
1947-
"'b' has dtype: object, cannot use method 'nsmallest' "
1948-
"with this dtype"
1949-
)
1950-
if 'b' in order:
1951-
with pytest.raises(TypeError) as exception:
1952-
df.nsmallest(n, order)
1953-
assert exception.value, error_msg
1965+
if order[0] == 'b':
1966+
1967+
# Only expect error when 'b' is first in order, as 'a' and 'c' are
1968+
# unique
1969+
error_msg = (
1970+
"'b' has dtype: object, cannot use method 'nsmallest' "
1971+
"with this dtype"
1972+
)
1973+
with pytest.raises(TypeError) as exc_info:
1974+
getattr(df, method)(n, order)
1975+
assert exc_info.value, error_msg
19541976
else:
1955-
result = df.nsmallest(n, order)
1956-
expected = df.sort_values(order).head(n)
1977+
ascending = method == 'nsmallest'
1978+
result = getattr(df, method)(n, order)
1979+
expected = df.sort_values(order, ascending=ascending).head(n)
19571980
tm.assert_frame_equal(result, expected)
19581981

1959-
if 'b' in order:
1960-
with pytest.raises(TypeError) as exception:
1961-
df.nsmallest(n, order)
1962-
assert exception.value, error_msg
1963-
else:
1964-
result = df.nlargest(n, order)
1965-
expected = df.sort_values(order, ascending=False).head(n)
1966-
tm.assert_frame_equal(result, expected)
1967-
1968-
def test_n_error(self, df_strings):
1969-
# b alone raises a TypeError
1970-
df = df_strings
1971-
with pytest.raises(TypeError):
1972-
df.nsmallest(1, 'b')
1973-
with pytest.raises(TypeError):
1974-
df.nlargest(1, 'b')
1982+
@pytest.mark.parametrize(
1983+
'method, columns',
1984+
product(['nsmallest', 'nlargest'],
1985+
product(['group'], ['category_string', 'string'])
1986+
))
1987+
def test_n_error(self, df_main_dtypes, method, columns):
1988+
df = df_main_dtypes
1989+
with pytest.raises(TypeError) as exc_info:
1990+
getattr(df, method)(2, columns)
1991+
msg = "Cannot use method '%s' with dtype %s" % (
1992+
method, df[columns[1]].dtype
1993+
)
1994+
assert exc_info.value, msg
1995+
1996+
def test_n_all_dtypes(self, df_main_dtypes):
1997+
df = df_main_dtypes
1998+
df.nsmallest(2, list(set(df) - {'category_string', 'string'}))
1999+
df.nsmallest(2, ['int', 'string']) # int column is unique => OK
2000+
df.nlargest(2, list(set(df) - {'category_string', 'string'}))
2001+
df.nlargest(2, ['int', 'string']) # int column is unique => OK
19752002

19762003
def test_n_identical_values(self):
19772004
# GH15297

pandas/tests/series/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,9 @@ class TestNLargestNSmallest(object):
16671667
Series([3., 2, 1, 2, 5], dtype='object'),
16681668
# not supported on some archs
16691669
# Series([3., 2, 1, 2, 5], dtype='complex256'),
1670-
Series([3., 2, 1, 2, 5], dtype='complex128')])
1670+
Series([3., 2, 1, 2, 5], dtype='complex128'),
1671+
Series(list('abcde'), dtype='category'),
1672+
Series(list('abcde'))])
16711673
def test_error(self, r):
16721674
dt = r.dtype
16731675
msg = "Cannot use method 'n(larg|small)est' with dtype %s" % dt

0 commit comments

Comments
 (0)