0

I would like to get that entries of an datetime numpy array back that are bigger then my input datetime variable.

Unfortunately, I get this error when executing the code below:

TypeError: '>' not supported between instances of 'int' and 'datetime.datetime'

This is my code:

import numpy as np
import pandas as pd
myRange = pd.date_range('2018-04-09', periods=5, freq='1D20min')

myArray = np.array(myRange).astype(np.datetime64).reshape(-1,1)
print("myArray:", myArray)
myDatetime = pd.datetime(2018,4,10,2,59,59)

myArray[myArray>myDatetime]

.

myArray: [['2018-04-09T00:00:00.000000000']
 ['2018-04-10T00:20:00.000000000']
 ['2018-04-11T00:40:00.000000000']
 ['2018-04-12T01:00:00.000000000']
 ['2018-04-13T01:20:00.000000000']]
1
  • you are trying to use boolean indexing, but the syntax is incorrect. Commented Jan 19, 2019 at 1:12

1 Answer 1

1

The problem is around comparing:
myArray (of type np.datetime64) with
myDateTime (of type pd.datetime)

Changing myDateTime to a numpy datetime64 gives a result.

import numpy as np
import pandas as pd
myRange = pd.date_range('2018-04-09', periods=5, freq='1D20min')

myArray = np.array(myRange).astype(np.datetime64).reshape(-1,1)
print("myArray:", myArray)
myDatetime = np.datetime64("2018-04-10T02:59:59")

myArray[myArray>myDatetime]

gives:

myArray: [['2018-04-09T00:00:00.000000000']
['2018-04-10T00:20:00.000000000']
['2018-04-11T00:40:00.000000000']
['2018-04-12T01:00:00.000000000']
['2018-04-13T01:20:00.000000000']]
Out[27]: 
array(['2018-04-11T00:40:00.000000000', 
       '2018-04-12T01:00:00.000000000',
       '2018-04-13T01:20:00.000000000'], dtype='datetime64[ns]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.