Skip to content

Commit dd771e6

Browse files
author
Roger Thomas
committed
Add More Error Tests
1 parent c86ce45 commit dd771e6

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

pandas/tests/frame/test_analytics.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,12 +1966,35 @@ def test_n(self, df_strings, n, order):
19661966
tm.assert_frame_equal(result, expected)
19671967

19681968
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')
1969+
df = pd.DataFrame(
1970+
{'group': [1, 1, 2],
1971+
'int': [1, 2, 3],
1972+
'float': [4., 5., 6.],
1973+
'string': list('abc'),
1974+
'category_string': pd.Series(list('abc')).astype('category'),
1975+
'category_int': [7, 8, 9],
1976+
'datetime': pd.date_range('20130101', periods=3),
1977+
'datetimetz': pd.date_range('20130101',
1978+
periods=3,
1979+
tz='US/Eastern'),
1980+
'timedelta': pd.timedelta_range('1 s', periods=3, freq='s')},
1981+
columns=['group', 'int', 'float', 'string',
1982+
'category_string', 'category_int',
1983+
'datetime', 'datetimetz',
1984+
'timedelta'])
1985+
columns_with_errors = {'category_string', 'string'}
1986+
columns_without_errors = list(set(df) - columns_with_errors)
1987+
for column in columns_with_errors:
1988+
with pytest.raises(TypeError):
1989+
df.nsmallest(2, column)
1990+
with pytest.raises(TypeError):
1991+
df.nsmallest(2, ['group', column])
1992+
with pytest.raises(TypeError):
1993+
df.nlargest(2, column)
1994+
with pytest.raises(TypeError):
1995+
df.nlargest(2, ['group', column])
1996+
df.nsmallest(2, columns_without_errors)
1997+
df.nsmallest(2, ['int', 'string']) # int column is unique => OK
19751998

19761999
def test_n_identical_values(self):
19772000
# GH15297

0 commit comments

Comments
 (0)