Trying_hard
Trying_hard

Reputation: 9501

Pandas time Slices

I am trying to read a csv and then return a dataframe based off a time slice.

Date    Time    Transaction No.
7/3/2017    15:26:54    2141439
7/3/2017    15:26:44    2142226
7/3/2017    15:25:26    2132076
7/3/2017    15:24:17    2141038
7/3/2017    15:20:55    2140073
7/3/2017    15:16:28    2133436
7/3/2017    15:10:48    2134561
7/3/2017    14:40:26    2125436
7/3/2017    14:35:50    2133569
7/3/2017    14:33:18    2126370
7/3/2017    14:31:42    2132945

Above is what my csv looks like I am trying the following:

df = pd.read_csv('test_file.csv',parse_dates=[['Date', 'Time']])

I would want to return only values between 14:30 and 15:00. I tried adding index_col =[0] to the read_csv function but I am getting a little lost of how to return the data I am looking. My initial thought was to make my index be a datetime index and use the between function to get what I desired but I can't seem to make it work.

Upvotes: 1

Views: 2319

Answers (1)

jezrael
jezrael

Reputation: 862681

Use parameter index_col for DatetimeIndex and then filter by between_time:

df = pd.read_csv('test_file.csv',index_col=['Date_Time'], parse_dates=[['Date', 'Time']])
print (df)
                     Transaction No.
Date_Time                           
2017-07-03 15:26:54          2141439
2017-07-03 15:26:44          2142226
2017-07-03 15:25:26          2132076
2017-07-03 15:24:17          2141038
2017-07-03 15:20:55          2140073
2017-07-03 15:16:28          2133436
2017-07-03 15:10:48          2134561
2017-07-03 14:40:26          2125436
2017-07-03 14:35:50          2133569
2017-07-03 14:33:18          2126370
2017-07-03 14:31:42          2132945


df = df.between_time('14:30', '15:00')
print (df)
                     Transaction No.
Date_Time                           
2017-07-03 14:40:26          2125436
2017-07-03 14:35:50          2133569
2017-07-03 14:33:18          2126370
2017-07-03 14:31:42          2132945

Upvotes: 1

Related Questions