我尝试使用以下csv数据获取经过的时间,但是当日期交叉时,Elapsed_time中的秒数显示为减号。
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import pylab as pl
import pandas as pd
dft = pd.read_csv("/content/drive/MyDrive/toa_data/to_csv_out_columns.csv", encoding="UTF-8")
dft["Time"] = pd.to_datetime(dft["Time"])
dft["second"] = pd.to_timedelta(dft["Time"].dt.time.astype(str)).dt.total_seconds()#.div(60)
dft = dft.set_index("second")
#dft.index = pd.to_datetime(dft.index)
dft['diff'] = dft.index.to_series().diff()
dft["Elapsed_time"]=dft["diff"].cumsum()
dft.head(5)
second Time Elapsed_time value diff
72446 2023-04-22 20:07:26 3.16
72506 2023-04-22 20:08:26 60 3.16 60
72566 2023-04-22 20:09:26 120 3.17 60
72626 2023-04-22 20:10:26 180 3.18 60
--------------omission-----------------------------------
86306 2023-04-22 23:58:26 13860 3.41 60
86366 2023-04-22 23:59:26 13920 3.41 60
26 2023-04-23 0:00:26 -72420 3.41 -86340
86 2023-04-23 0:01:26 -72360 3.41 60
146 2023-04-23 0:02:26 -72300 3.41 60
--------------------------------------------------
我想输出以下csv。
second Time Elapsed_time value diff
72446 2023-04-22 20:07:26 3.16
72506 2023-04-22 20:08:26 60 3.16 60
72566 2023-04-22 20:09:26 120 3.17 60
72626 2023-04-22 20:10:26 180 3.18 60
--------------omission----------------------------
86306 2023-04-22 23:58:26 13860 3.41 60
86366 2023-04-22 23:59:26 13920 3.41 60
86426 2023-04-23 0:00:26 13960 3.41 60
86486 2023-04-23 0:01:26 14020 3.41 60
86546 2023-04-23 0:02:26 14080 3.41 60
--------------------------------------------------
2条答案
按热度按时间wfsdck301#
我想你想要:
输出:
z4iuyo4d2#
您需要正确计算行之间的时间差,使用
pd.Series.shift()
。示例:
将返回: