【发布时间】:2022-01-08 18:23:38
【问题描述】:
我正在尝试
- 从 df 列中的唤醒时间列表中获取平均唤醒时间,然后
- 将它们与目标唤醒时间(更早或更晚)进行比较
import pandas as pd
#convert to datetime object
df['wakeup_time_date']=pd.to_datetime(df['wakeup_time_date'], infer_datetime_format=True)
#extract time
df['wakeup_time']=df['wakeup_time_date'].dt.time
#below doesn't work as the object is no longer a datetime object
df['wakeup_time'].mean()
最后,我想将平均唤醒时间与早上 6:30 进行比较,并确定是早还是晚。
【问题讨论】:
-
不要通过调用
.dt.time来删除日期,只需忽略它,如下所示:Mean of time component only of pandas datetime column -
这是平均时间。但是我仍然不确定如何解决任务的第二部分:将该时间与目标时间进行比较。