【发布时间】:2023-03-22 22:00:02
【问题描述】:
我想对数据框中的列应用条件。该列具有HH:MM:SS 格式的时间值,我想将其与固定时间值(阈值)进行比较,但出现错误。将阈值转换为datetime.datetime.strptime 会给我错误。我想问的是 - 为了比较任何两个时间值,我如何存储它们或者我将它们转换成什么以便可以比较它们?阈值是字符串,列值是datetime.time格式。
当我这样做时-
threshold = datetime.datetime.strptime('1:04:00','%H:%M:%S').time()
df_styled = df.style.apply(lambda x:['background:red' if x > threshold else "background:white" for x in df['Total Time']], axis=0)
我明白了-
TypeError:“datetime.datetime”和“datetime.time”实例之间不支持“>”
当我这样做时-
threshold = datetime.datetime.strptime('1:04:00','%H:%M:%S')
df['Total Time'] = df['Total Time'].apply(lambda x: datetime.datetime.strptime(x,'%H:%M:%S'))
df_styled = df.style.apply(lambda x:['background:red' if x > threshold else 'background:white' for x in df['Total Time']], axis=0)
我得到了错误
TypeError: strptime() 参数 1 必须是 str,而不是 datetime.time
【问题讨论】:
标签: python pandas datetime timedelta