简体   繁体   English

在数据框python中找到时间戳之间的差异时出错

[英]Error in finding the delta between timestamps in dataframe python

I'm trying to extract timestamp data present in a dataframe and find the time difference in seconds between the time intervals. 我正在尝试提取数据帧中存在的时间戳数据,并找到时间间隔之间以秒为单位的时间差。 Using this time difference, I want to calculate the speed of some object (Speed = Distance/Time). 使用这个时差,我想计算某些物体的速度(速度=距离/时间)。
However, I'm getting a lot of errors in this time difference calculation and I'm not able to directly consider the Delta value. 但是,在此时差计算中出现很多错误,因此无法直接考虑Delta值。 Can someone help me figure out what's wrong or missing in my implementation? 有人可以帮我弄清楚我的实现中有什么问题或遗漏吗?
Thanks! 谢谢!

import gpxpy
import datetime
import pandas as pd

with open('test3.gpx') as fh:
    gpx_file = gpxpy.parse(fh)
    segment = gpx_file.tracks[0].segments[0]
    coords = pd.DataFrame([
    { 'time': p.time} for p in segment.points])

dist1 = 1
coords['timestamp'] = [datetime.datetime.time(d) for d in coords['time']]
timedelta = [datetime.datetime.combine(datetime.date.today(), coords.timestamp[i + 1]) - datetime.datetime.combine(datetime.date.today(), coords.timestamp[i]) for i in range(len(coords.time)-1)]
speed = dist1/timedelta

GPX File for reference: https://github.com/stevenvandorpe/testdata/blob/master/gps_coordinates/gpx/my_run_001.gpx GPX文件供参考: https : //github.com/stevenvandorpe/testdata/blob/master/gps_coordinates/gpx/my_run_001.gpx

Error1: 错误1:
This is because the denominator is of type datetime.timedelta. 这是因为分母的类型为datetime.timedelta。

 TypeError: unsupported operand type(s) for /: 'int' and 'list'

Error2: 错误2:
I also tried converting the list Elements to integer, but it does not directly allow the conversion for datetime list elements 我也尝试将列表元素转换为整数,但是它不允许直接转换日期时间列表元素

tdelta1 = [int(x) for x in timedelta]
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.timedelta'

I'd suggest using the date time datatype built into numpy and supported by Pandas, eg: 我建议使用内置在numpy中并受Pandas支持的日期时间数据类型,例如:

import gpxpy
import pandas as pd

with open('my_run_001.gpx') as fd:
    gpx_file = gpxpy.parse(fd)
    segment = gpx_file.tracks[0].segments[0]

    times = pd.Series([p.time for p in segment.points], name='time')

reads the values in from your file, and then you can diff and convert to second intervals easily with: 从文件中读取值,然后可以使用以下方法轻松进行比较并转换为第二个间隔:

import numpy as np

dt = np.diff(times.values) / np.timedelta64(1, 's')
speed = 1 / dt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM