#NEWLINE# def test_get_loc( self ) : #NEWLINE# #INDENT# idx = pd . date_range ( '2000-01-01' , periods = 3 ) #NEWLINE# for method in [ None , 'pad' , 'backfill' , 'nearest' ] : #NEWLINE# #INDENT# self . assertEqual ( idx . get_loc ( idx [ 1 ] , method ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( idx [ 1 ] . to_pydatetime ( ) , method ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( str ( idx [ 1 ] ) , method ) , 1 ) #NEWLINE# if ( method is not None ) : #NEWLINE# #INDENT# self . assertEqual ( idx . get_loc ( idx [ 1 ] , method , tolerance = pd . Timedelta ( '0 days' ) ) , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# self . assertEqual ( idx . get_loc ( '2000-01-01' , method = 'nearest' ) , 0 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-01T12' , method = 'nearest' ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-01T12' , method = 'nearest' , tolerance = '1 day' ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-01T12' , method = 'nearest' , tolerance = pd . Timedelta ( '1D' ) ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-01T12' , method = 'nearest' , tolerance = np . timedelta64 ( 1 , 'D' ) ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-01T12' , method = 'nearest' , tolerance = timedelta ( 1 ) ) , 1 ) #NEWLINE# with tm . assertRaisesRegexp ( ValueError , 'must be convertible' ) : #NEWLINE# #INDENT# idx . get_loc ( '2000-01-01T12' , method = 'nearest' , tolerance = 'foo' ) #NEWLINE# #UNINDENT# with tm . assertRaises ( KeyError ) : #NEWLINE# #INDENT# idx . get_loc ( '2000-01-01T03' , method = 'nearest' , tolerance = '2 hours' ) #NEWLINE# #UNINDENT# self . assertEqual ( idx . get_loc ( '2000' , method = 'nearest' ) , slice ( 0 , 3 ) ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01' , method = 'nearest' ) , slice ( 0 , 3 ) ) #NEWLINE# self . assertEqual ( idx . get_loc ( '1999' , method = 'nearest' ) , 0 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2001' , method = 'nearest' ) , 2 ) #NEWLINE# with tm . assertRaises ( KeyError ) : #NEWLINE# #INDENT# idx . get_loc ( '1999' , method = 'pad' ) #NEWLINE# #UNINDENT# with tm . assertRaises ( KeyError ) : #NEWLINE# #INDENT# idx . get_loc ( '2001' , method = 'backfill' ) #NEWLINE# #UNINDENT# with tm . assertRaises ( KeyError ) : #NEWLINE# #INDENT# idx . get_loc ( 'foobar' ) #NEWLINE# #UNINDENT# with tm . assertRaises ( TypeError ) : #NEWLINE# #INDENT# idx . get_loc ( slice ( 2 ) ) #NEWLINE# #UNINDENT# idx = pd . to_datetime ( [ '2000-01-01' , '2000-01-04' ] ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-02' , method = 'nearest' ) , 0 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01-03' , method = 'nearest' ) , 1 ) #NEWLINE# self . assertEqual ( idx . get_loc ( '2000-01' , method = 'nearest' ) , slice ( 0 , 2 ) ) #NEWLINE# idx = pd . date_range ( '2000-01-01' , periods = 24 , freq = 'H' ) #NEWLINE# tm . assert_numpy_array_equal ( idx . get_loc ( time ( 12 ) ) , [ 12 ] ) #NEWLINE# tm . assert_numpy_array_equal ( idx . get_loc ( time ( 12 , 30 ) ) , [ ] ) #NEWLINE# with tm . assertRaises ( NotImplementedError ) : #NEWLINE# #INDENT# idx . get_loc ( time ( 12 , 30 ) , method = 'pad' )