【发布时间】:2015-07-13 10:15:01
【问题描述】:
我正在尝试生成一个采样频率为 5120 Hz 的pandas.DateTimeIndex。这给出了increment=0.0001953125 秒的时间段。
如果您尝试使用pandas.date_range(),则需要将频率(参数freq)指定为str 或pandas.DateOffset。第一个只能处理高达1 ns的精度,后者与str相比性能很差,误差更严重。
使用字符串时,我的构造如下:
freq=str(int(increment*1e9))+'N')
它在不到 2 秒的时间内执行了我的 270 Mb 文件,但在大约 1500 µs 的 300 万条记录后出现错误(在 DateTimeIndex 中)。
使用pandas.DateOffset时,像这样
freq=pd.DateOffset(seconds=increment)
它在1分14秒内解析文件,但有大约一秒的错误。
我还尝试使用构造DateTimeIndex
starttime + pd.to_timedelta(cumulativeTimes, unit='s')
这个总和也需要很长时间才能完成,但它是唯一一个在生成的DateTimeIndex 中没有错误的总和。
如何实现DateTimeIndex 的高效生成,同时保持我的准确性?
【问题讨论】:
-
如果 Pandas 中似乎存在错误,您可以在 github.com/pydata/pandas/issues 上提出问题吗?
-
@joris 我以为 pandas 中存在错误,但我不这么认为了。只是 pandas 的精度为 1 ns,其他的都是四舍五入造成的。
标签: python python-3.x pandas