Skip to content

Commit af493a7

Browse files
author
Roger Thomas
committed
Update Tests
1 parent 4d7a7c2 commit af493a7

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

pandas/tests/frame/test_analytics.py

+56-52
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,62 +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-
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-
methods = 'nsmallest', 'nlargest'
1988-
for col in columns_with_errors:
1989-
for method, cols in product(methods, (col, ['group', col])):
1990-
with pytest.raises(TypeError) as exc_info:
1991-
getattr(df, method)(2, cols)
1992-
msg = "Cannot use method '%s' with dtype %s" % (
1993-
method, df[col].dtype
1994-
)
1995-
assert exc_info.value, msg
1996-
df.nsmallest(2, columns_without_errors)
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'}))
19971999
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
19982002

19992003
def test_n_identical_values(self):
20002004
# GH15297

0 commit comments

Comments
 (0)