#NEWLINE# def test_nsmallest_nlargest( self ) : #NEWLINE# #INDENT# base = [ 3 , 2 , 1 , 2 , 5 ] #NEWLINE# s_list = [ Series ( base , dtype = 'int8' ) , Series ( base , dtype = 'int16' ) , Series ( base , dtype = 'int32' ) , Series ( base , dtype = 'int64' ) , Series ( base , dtype = 'float32' ) , Series ( base , dtype = 'float64' ) , Series ( base , dtype = 'uint8' ) , Series ( base , dtype = 'uint16' ) , Series ( base , dtype = 'uint32' ) , Series ( base , dtype = 'uint64' ) , Series ( base ) . astype ( 'timedelta64[ns]' ) , Series ( pd . to_datetime ( [ '2003' , '2002' , '2001' , '2002' , '2005' ] ) ) ] #NEWLINE# raising = [ Series ( [ 3.0 , 2 , 1 , 2 , '5' ] , dtype = 'object' ) , Series ( [ 3.0 , 2 , 1 , 2 , 5 ] , dtype = 'object' ) , Series ( [ 3.0 , 2 , 1 , 2 , 5 ] , dtype = 'complex128' ) ] #NEWLINE# for r in raising : #NEWLINE# #INDENT# dt = r . dtype #NEWLINE# msg = ( "Cannot use method 'n(larg|small)est' with dtype %s" % dt ) #NEWLINE# args = ( 2 , len ( r ) , 0 , ( - 1 ) ) #NEWLINE# methods = ( r . nlargest , r . nsmallest ) #NEWLINE# for ( method , arg ) in product ( methods , args ) : #NEWLINE# #INDENT# with tm . assertRaisesRegexp ( TypeError , msg ) : #NEWLINE# #INDENT# method ( arg ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for s in s_list : #NEWLINE# #INDENT# assert_series_equal ( s . nsmallest ( 2 ) , s . iloc [ [ 2 , 1 ] ] ) #NEWLINE# assert_series_equal ( s . nsmallest ( 2 , keep = 'last' ) , s . iloc [ [ 2 , 3 ] ] ) #NEWLINE# with tm . assert_produces_warning ( FutureWarning ) : #NEWLINE# #INDENT# assert_series_equal ( s . nsmallest ( 2 , take_last = True ) , s . iloc [ [ 2 , 3 ] ] ) #NEWLINE# #UNINDENT# assert_series_equal ( s . nlargest ( 3 ) , s . iloc [ [ 4 , 0 , 1 ] ] ) #NEWLINE# assert_series_equal ( s . nlargest ( 3 , keep = 'last' ) , s . iloc [ [ 4 , 0 , 3 ] ] ) #NEWLINE# with tm . assert_produces_warning ( FutureWarning ) : #NEWLINE# #INDENT# assert_series_equal ( s . nlargest ( 3 , take_last = True ) , s . iloc [ [ 4 , 0 , 3 ] ] ) #NEWLINE# #UNINDENT# empty = s . iloc [ 0 : 0 ] #NEWLINE# assert_series_equal ( s . nsmallest ( 0 ) , empty ) #NEWLINE# assert_series_equal ( s . nsmallest ( ( - 1 ) ) , empty ) #NEWLINE# assert_series_equal ( s . nlargest ( 0 ) , empty ) #NEWLINE# assert_series_equal ( s . nlargest ( ( - 1 ) ) , empty ) #NEWLINE# assert_series_equal ( s . nsmallest ( len ( s ) ) , s . sort_values ( ) ) #NEWLINE# assert_series_equal ( s . nsmallest ( ( len ( s ) + 1 ) ) , s . sort_values ( ) ) #NEWLINE# assert_series_equal ( s . nlargest ( len ( s ) ) , s . iloc [ [ 4 , 0 , 1 , 3 , 2 ] ] ) #NEWLINE# assert_series_equal ( s . nlargest ( ( len ( s ) + 1 ) ) , s . iloc [ [ 4 , 0 , 1 , 3 , 2 ] ] ) #NEWLINE# #UNINDENT# s = Series ( [ 3.0 , np . nan , 1 , 2 , 5 ] ) #NEWLINE# assert_series_equal ( s . nlargest ( ) , s . iloc [ [ 4 , 0 , 3 , 2 ] ] ) #NEWLINE# assert_series_equal ( s . nsmallest ( ) , s . iloc [ [ 2 , 3 , 0 , 4 ] ] ) #NEWLINE# msg = 'keep must be either "first", "last"' #NEWLINE# with tm . assertRaisesRegexp ( ValueError , msg ) : #NEWLINE# #INDENT# s . nsmallest ( keep = 'invalid' ) #NEWLINE# #UNINDENT# with tm . assertRaisesRegexp ( ValueError , msg ) : #NEWLINE# #INDENT# s . nlargest ( keep = 'invalid' )