wihee
wihee

Reputation: 85

Get rows from dataframe between specific time (hours and minutes)

I want to add to a dataframe different shifts in a additional column in a df to group data by different shifts. I have a dataframe like this.

chat_time              time      time_h   time_m
2022-09-07 10:58:04    10:58:04  10       58
2022-09-07 12:36:43    12:36:43  12       36
the chat_time column is not the index as some times are exactly the same(up to seconds) so it would produce duplicates.

now what i was trying is to add a column called 'shift' and to this column i want to add 'first' for all rows where the chat_time is between 10:30 and 13:00.

So i was trying to extract all rows which are in between this time add first to the column and then concat it so it adds it to the df. In total i want to add for different shifts based in the time.

I was hoping to get all rows based in the time column but when i extract time from datetime it converts it to an object. that's why I added to additional columns which are int (time_h and time_m) representing the hour and minute. But when i want to slice it so that i get all rows in this time range it gives me only chats where minute is over 30 i use the following code

first = all_mes_3[(((all_mes_3['time_h']>=10) & (all_mes_3['time_m']>=30)) & ((all_mes_3['time_h']<13)))]

I am stuck because I want to have chats starting from 10:30 and ending at 13:00 and not only the chate in this time which are over time_m 30.

Upvotes: 1

Views: 2121

Answers (1)

chitown88
chitown88

Reputation: 28565

Easiest way is to use pandas' .between_time().

import pandas as pd


cols = ['chat_time', 'time', 'time_h', 'time_m']
data = [
        ['2022-09-07 10:58:04','10:58:04','10','58'],
        ['2022-09-07 12:36:43','12:36:43','12','36'],
        ['2022-09-07 14:36:43','14:36:43','14','36']]

df = pd.DataFrame(data, columns=cols)
df['chat_time'] = pd.to_datetime(df['chat_time'])
df.index = df['chat_time']



df_filter = df.between_time('10:30', '13:00').reset_index(drop=True)
        

Upvotes: 2

Related Questions