Home > OS >  How to drop rows between two times?
How to drop rows between two times?

Time:09-26

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!

CodePudding user response:

use:

df["Timestamp"] = pd.to_datetime(df["Timestamp"])
mask = (df['Timestamp'].dt.hour > 4) & (df['Timestamp'].dt.hour < 22)
df=df[mask]

CodePudding user response:

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.

  • Related