Pandas get_loc with datetimeindex failing

女生的网名这么多〃 提交于 2021-01-29 10:17:45

问题


I have a time-indexed dataframe, and I want to find what row number matches the closest time to a given time.

For example,:

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')

However, this throws an error:

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

If it matters, this is Pandas 1.0.1, on python 3.8.1 64-bit on Windows 10 1909.

What am I doing wrong here?

来源:https://stackoverflow.com/questions/60330253/pandas-get-loc-with-datetimeindex-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!