@@ -1966,12 +1966,35 @@ def test_n(self, df_strings, n, order):
1966
1966
tm .assert_frame_equal (result , expected )
1967
1967
1968
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' )
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
1975
1998
1976
1999
def test_n_identical_values (self ):
1977
2000
# GH15297
0 commit comments