Subject.iloc[1, 0] is equal to datetime.time(13, 16, 14, 336000)和Subject.iloc[2, 0] is equal to datetime.time(13, 16, 14, 338000)我要做的就是找到從 Subject.iloc[1, 0] 到 Subject.iloc[2, 0] 的經過時間。但是當我減去它們時,它會說Subject.iloc[1, 0]-Subject.iloc[2, 0]TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'但后來我想也許我應該使用時間增量 pd.to_timedelta(Subject.iloc[2, 0].astype(str))上面寫著 AttributeError: 'datetime.time' 對象沒有屬性 'astype'誰能幫我這個?我做錯了什么?甚至這個pd.to_timedelta(Subject.iloc[2, 0]) won't work
2 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
你可以嘗試這樣做
d1 = datetime.datetime.combine(datetime.date.today(), Subject.iloc[2, 0])
d2 = datetime.datetime.combine(datetime.date.today(), Subject.iloc[1, 0])
diff = d1 - d2
要使用 Pandas' to_timedelta,請執行以下操作
pd.to_timedelta(str(Subject.iloc[2, 0]))

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
實際上,我最近不得不這樣做。您可以將日期時間(或在我的情況下的時間結構對象)轉換為“自紀元以來的秒數”,然后進行比較。
在 Python 中,如何將 `datetime` 對象轉換為秒?
對我來說,情況更復雜:
durSec = time.mktime(time.strptime(data[7], "%Y-%m-%d %H:%M:%S")) - time.mktime(time.strptime(data[6], "%Y-%m-%d %H:%M:%S"))
添加回答
舉報
0/150
提交
取消