首页 > 解决方案 > 不可散列的类型:将 GPS 数据加载到 pandas 数据帧时的“SimpleTZ”

问题描述

我一直在用 R 解析一些 GPS 轨迹,并认为我会在 Python 中尝试同样的事情,尽管我的 Python 技能要弱得多。

我正在通过https://towardsdatascience.com/how-tracking-apps-analysis-your-gps-data-a-hands-on-tutorial-in-python-756d4db6715d

一切顺利,直到

df = pd.DataFrame(columns=['lon', 'lat', 'alt', 'time'])
for point in data:
    df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : point.time}, ignore_index=True)

这给了我一个错误,以

TypeError:不可散列的类型:'SimpleTZ'

一个 GPS 点如下所示:

GPXTrackPoint(40.08285088, -75.19106663, elevation=144.4, time=datetime.datetime(2019, 10, 22, 18, 36, 41, tzinfo=SimpleTZ("Z")))

我在 Windows 上通过 Anaconda 在 Jupyter 中使用 Python 3.7.0。

我更喜欢用区域解析时间戳,但如果这会让事情变得更容易,我愿意丢弃区域。

标签: pythonpandasdataframetimegis

解决方案


我觉得这更像是一个熊猫问题而不是 gpxpy 问题。甚至是一般的时区问题?

无论如何,这种与https://ocefpaf.github.io/python4oceanographers/blog/2014/08/18/gpx/略有不同的方法确实有效

data = []
gpx = gpxpy.parse(open(gpxfile))
track = gpx.tracks[0]
segment = track.segments[0]

for point_idx, point in enumerate(segment.points):
    data.append([point.longitude, point.latitude,
                 point.elevation, point.time, segment.get_speed(point_idx)])

columns = ['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed']
df = DataFrame(data, columns=columns) 

推荐阅读