@@ -1916,13 +1916,33 @@ def df_strings():
1916
1916
'c' : np .random .permutation (10 ).astype ('float64' )})
1917
1917
1918
1918
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
+
1919
1939
class TestNLargestNSmallest (object ):
1920
1940
1921
1941
# ----------------------------------------------------------------------
1922
1942
# Top / bottom
1923
1943
@pytest .mark .parametrize (
1924
- 'n, order' ,
1925
- product (range (1 , 11 ),
1944
+ 'method, n, order' ,
1945
+ product ([ 'nsmallest' , 'nlargest' ], range (1 , 11 ),
1926
1946
[['a' ],
1927
1947
['c' ],
1928
1948
['a' , 'b' ],
@@ -1939,39 +1959,46 @@ class TestNLargestNSmallest(object):
1939
1959
['b' , 'c' , 'c' ],
1940
1960
1941
1961
]))
1942
- def test_n (self , df_strings , n , order ):
1962
+ def test_n (self , df_strings , method , n , order ):
1943
1963
# GH10393
1944
1964
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
1954
1976
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 )
1957
1980
tm .assert_frame_equal (result , expected )
1958
1981
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
1975
2002
1976
2003
def test_n_identical_values (self ):
1977
2004
# GH15297
0 commit comments