I have a python code that converts one column df[r_t] to utc:
df.r_t = pd.to_datetime(df.r_t)
And generate another column for time:
df['time'] = (df['r_t'].dt.time)
My resulting column is as follows:
| r_t | time |
| ----------------------- | --------- |
| 2022-05-28 00:00:00 00 | 00:00:00 |
| 2022-05-28 00:00:01 00 | 00:00:01 |
| 2022-05-28 00:00:02 00 | 00:00:02 |
We have 20k rows. Now what am trying to do is generate a night and day dataframe: Like
time_1 = pd.to_datetime('04:00:00')
time_2 = pd.to_datetime('09:00:00')
df_day = df[(df['time'] > time_1) & (df['time'] <= time_2)]
df_night = not in(df[(df['time'] > time_1) & (df['time'] <= time_2)])
I know am doing this wrong as i get error:
TypeError: '>' not supported between instances of 'datetime.time' and 'Timestamp'
Can someone point me in the right direction. Thanks!
CodePudding user response:
Try:
time_day = pd.to_datetime("04:00:00").time()
time_night = pd.to_datetime("09:00:00").time()
mask = (df["time"] > time_day) & (df["time"] <= time_night)
df_day = df[mask]
df_night = df[~mask]