0

I have two pandas dataframes, both with datetime indices; sample output:

l1_with_nonlimiting_trace_gas_df.index:

DatetimeIndex(['2018-03-25 11:13:41+00:00', '2018-03-25 11:17:40+00:00',
               '2018-03-25 11:21:38+00:00', '2018-03-25 11:25:23+00:00',
               '2018-03-25 11:29:03+00:00', '2018-03-25 11:34:06+00:00',
               '2018-03-25 11:37:47+00:00', '2018-03-25 11:48:48+00:00',
               '2018-03-25 11:59:23+00:00', '2018-03-25 12:08:32+00:00',
               ...
               '2018-03-25 22:31:00+00:00', '2018-03-25 22:45:34+00:00',
               '2018-03-25 22:55:34+00:00', '2018-03-25 23:04:55+00:00',
               '2018-03-25 23:12:44+00:00', '2018-03-25 23:16:38+00:00',
               '2018-03-25 23:20:38+00:00', '2018-03-25 23:24:33+00:00',
               '2018-03-25 23:28:32+00:00', '2018-03-25 23:33:36+00:00'],
              dtype='datetime64[ns, UTC]', length=124, freq=None)

l1_with_limiting_trace_gas_df.index:

DatetimeIndex(['2018-03-25 11:11:38+00:00', '2018-03-25 11:15:39+00:00',
               '2018-03-25 11:19:38+00:00', '2018-03-25 11:23:27+00:00',
               '2018-03-25 11:27:11+00:00', '2018-03-25 11:32:21+00:00',
               '2018-03-25 11:35:55+00:00', '2018-03-25 11:40:09+00:00',
               '2018-03-25 11:49:39+00:00', '2018-03-25 12:00:05+00:00',
               ...
               '2018-03-25 22:46:17+00:00', '2018-03-25 22:56:26+00:00',
               '2018-03-25 23:05:53+00:00', '2018-03-25 23:10:49+00:00',
               '2018-03-25 23:14:42+00:00', '2018-03-25 23:18:42+00:00',
               '2018-03-25 23:22:36+00:00', '2018-03-25 23:26:31+00:00',
               '2018-03-25 23:31:33+00:00', '2018-03-25 23:35:34+00:00'],
              dtype='datetime64[ns, UTC]', length=130, freq=None)

When I do the get_loc operation:

index_location=l1_with_nonlimiting_trace_gas_df.index.get_loc(l1_with_limiting_trace_gas_df.index[i],method='nearest')

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

That used to work in the past. Now it no longer works. Here are my versions:

pd.version Out[152]: '1.0.1'

np.version Out[154]: '1.18.1'

Some help here would be greatly appreciated.

bernski
  • 33
  • 7
  • I'd first try to update pandas to 1.1.1. – Trenton McKinney Aug 28 '20 at 19:15
  • Sadly my Anaconda Pandas is as up to date as it will allow. – bernski Aug 28 '20 at 19:33
  • You might consider installing the current Anaconda with python 3.8. – Trenton McKinney Aug 28 '20 at 19:36
  • The error is telling you that one argument (column) is object dtype, not `datetime64`. A full traceback might help, since you aren't actually taking a difference. But `get_loc` with `nearest` method might be doing so. – hpaulj Aug 28 '20 at 20:10
  • Thank you Trenton, that did the trick. This is one of the pitfalls of blowing dust off a project that has sat idle for months - so many new errors with new versions. – bernski Aug 28 '20 at 20:15
  • Hi hpaulj, both were definitely dtype datetime64 objects and hadn't given me any problems in the past. Clearly something in an interim update somewhere wasn't happy. Updating yet again today fixed the 'under-the-hood' issue. But thanks for the response. – bernski Aug 28 '20 at 20:19

1 Answers1

0

Take a look at Pandas get_loc with datetimeindex failing

Just the same problem has been reported and the last comment contains explanation that this problem occurs in Pandas version 1.0.1 (just your version).

I have version 1.0.3 and e.g. the following code works:

start = pd.Timestamp('2019-12-12 00:00')
end   = pd.Timestamp('2019-12-12 00:03')
testindex = pd.date_range(start, end, freq='10s')
findtime = pd.Timestamp('2019-12-12 0:01:16')
testindex.get_loc(findtime, method='nearest')

I got result 8.

So upgrade your Pandas.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41