gathering data in a time range

Hello all,

I need to gather the rows of data that happened between an specific time range (from 8:30 to 17:00). This is what I have done so far and it is not giving me any errors but it doesn’t work either. Thank you all!

df=pd.read_csv(“document.csv”)
df[‘Date-Time’] = pd.to_datetime(df[‘Date-Time’])
mask1=(df[‘Date-Time’] >= ‘08:30:00’) & (df[‘Date-Time’] <= ‘17:00:00’)
df[mask1]

First, you need to provide enough context - I only know what you’re trying to do from your last post.

Second, we don’t know what your input data looks like, so we don’t know what result to expect from the line “df[‘Date-Time’] = pd.to_datetime(df[‘Date-Time’])”.

My guess is, given your column is called ‘Date-Time’, you have both a date and a time in that column. For your time-range filtering to work, you want to have only the time value.

googling “pandas time from datetime” got me a few possibly useful links. E.g. something like this:

mask1=(df[‘Date-Time’].dt.time >= ‘08:30:00’) & (df[‘Date-Time’].dt.time <= ‘17:00:00’)