首页 > 解决方案 > 带有 datetimeindex 的 Pandas get_loc 失败

问题描述

我有一个时间索引数据框,我想找到与给定时间最接近的行号。

例如,:

import pandas as pd

findtime = pd.Timestamp('2019-12-12 10:19:25', tz='US/Eastern')
start = pd.Timestamp('2019-12-12 0:0:0', tz='US/Eastern')
end = pd.Timestamp('2019-12-13 0:0:0', tz='US/Eastern')
testindex = pd.date_range(start, end, freq='5s')
testindex.get_loc(findtime, method='nearest')

但是,这会引发错误:

UFuncTypeError: ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('O')

如果重要的话,这是 Pandas 1.0.1,在 Windows 10 1909 上的 python 3.8.1 64 位上。

我在这里做错了什么?

标签: pythonpandasdatetimeindex

解决方案


推荐阅读