Pandas获取\u loc,datetimeindex失败

2024-09-28 21:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个时间索引数据帧,我想找出与给定时间最近的时间匹配的行号

例如:

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,在Windows101909上的Python3.8.1 64位上

我做错了什么


Tags: 数据importpandas时间starttimestamptzend