Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 2

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]

1 Reply
Commander Lvl 2
Message 2 of 2

Re: gathering data in a time range

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')

 

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!