通过python从csv文件中减去时间值

问题描述 投票:0回答:1

我正在使用 pandas 读取 23:03:00 格式的时间序列的 csv 文件。 python 将其作为字符串读取,并且无法转换为整数或浮点数以从另一个时间步中减去它。

例如:

`df = pd.read_csv('test.csv', sep=";")

x=df.iloc[1,2]

y=df.iloc[1,2]

dif=y-x`

python dataframe time time-series data-conversion
1个回答
0
投票

您可以使用日期时间库中的 strptime 这是一个示例:

from datetime import datetime

date_string1 = "23:03:00"
date_string2 = "23:01:00"

date_object1 = datetime.strptime(date_string1, "%H:%M:%S")
date_object2 = datetime.strptime(date_string2, "%H:%M:%S")

difference = date_object1 - date_object2

print(difference) 

strptime的第二个参数是你的日期的格式。

© www.soinside.com 2019 - 2024. All rights reserved.