首页 > 解决方案 > 如何有效地将函数应用于大熊猫系列?

问题描述

我有一个以 unix 时间为开始日期的 pandas 系列时间样本。每个时间样本为 x * 1 / 512,因此时间戳 0 = 0,时间戳 2 = 1 / 512 或 0.00195,时间戳 3 = 2 / 512 或 0.0039。我需要将开始日期(偏移量)添加到所有值并将结果转换为本地时间(PST)。我有以下

times = np.arange(0, 3600, 1/512)
tz = 'US/Pacific'
offset = 1569603352 # 2019-09-27 09:57 (or something similar)
srs = pd.Series(times)
srs.apply(lambda t: pd.to_datetime(offset + t, unit='s', utc=True) \
                      .tz_convert(tz))

有什么方法可以加快速度吗?我有一堆强大的 GPU 和大约 50 个线程,因此可以使用多线程或处理。

标签: pythonpandastime-series

解决方案


我将通过删除加快速度apply

pd.to_datetime(offset + srs, unit='s', utc=True).dt.tz_convert(tz)

推荐阅读