How to drop rows between two times?

Question:

im doing measurements in a 10min interval every day. Now i want to exclude all measurements during the night. Therefore i extracted the Hour from the Timestamp to be able to exclude all rows/measurements between Hour 22 and 4.
My Dataframe:

Timestamp Hour Measurement1 Measurement2
2022-06-23 03:50:00 3.0 12.57 27.66
2022-06-23 04:00:00 4.0 12.78 27.89

My code:

df = df[(df.Hour < 22) | (df.Hour > 4)]

But this raises always an TypeError: ‘<‘ not supported between instances of ‘datetime.time’ and ‘float’

Hope for any ideas to make this work.
thank you!

Asked By: EF711

||

Answers:

This works for me:

df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df['Hour'] = df['Timestamp'].dt.hour
df['Hour'] > 4

To exclude rows for which the hour is between 22 and 4:

df = df[(df.Hour > 4) & (df.Hour < 22)]

Note that I am using & as the hour needs to be above 4 AND below 22.

Answered By: T C Molenaar

use:

df["Timestamp"] = pd.to_datetime(df["Timestamp"])
mask = (df['Timestamp'].dt.hour > 4) & (df['Timestamp'].dt.hour < 22)
df=df[mask]
Answered By: Clegane
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.