0

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?

ejuhjav
  • 2,660
  • 2
  • 21
  • 32
Andrew
  • 1
  • 1
  • What is "findtime", how is it defined? – cs95 Feb 20 '20 at 23:55
  • Fixed my original post, thanks. findtime = pd.Timestamp('2019-12-12 10:19:25', tz='US/Eastern') – Andrew Feb 21 '20 at 00:00
  • When `findtime` is defined like this, it works for me, I get 7433 as the result. I'm guessing `findtime` is created differently, wherever you get it from in your actual code. – cs95 Feb 21 '20 at 00:01
  • @cs95 solved. The problem is in pandas 1.0.1, it works in 0.25.3. – Andrew Feb 21 '20 at 02:24

0 Answers0