0

how can I write a code that shows me the index of where the Newdate1 and Newdate2 is located within Setups. The value for Newdate1 within Setups is the second index which outputs 1 for result. The np.where function does not work however. How could I do this without a for loop?

import numpy as np 

Setups = np.array(['2017-09-15T07:11:00.000000000', '2017-09-15T11:25:00.000000000',
                   '2017-09-15T12:11:00.000000000', '2017-12-22T03:14:00.000000000',
                   '2017-12-22T03:26:00.000000000', '2017-12-22T03:31:00.000000000',
                   '2017-12-22T03:56:00.000000000'],dtype="datetime64[ns]")
    
Newdate1 =  np.array(['2017-09-15T07:11:00.000000000'], dtype="datetime64[ns]")
Newdate2 =  np.array(['2017-12-22T03:26:00.000000000'], dtype="datetime64[ns]")

result = np.where(Setups == Newdate1)
result2 = np.where(Setups == Newdate2)

Expected Output:

result: 1
result2: 4

1 Answer 1

1

use np.in1d to pass the array to be searched within another array and get the indices using np.where.

import numpy as np 

Setups = np.array(['2017-09-15T07:11:00.000000000', '2017-09-15T11:25:00.000000000',
                   '2017-09-15T12:11:00.000000000', '2017-12-22T03:14:00.000000000',
                   '2017-12-22T03:26:00.000000000', '2017-12-22T03:31:00.000000000',
                   '2017-12-22T03:56:00.000000000'],dtype="datetime64[ns]")
    

newdates = np.array(['2017-09-15T07:11:00.000000000','2017-12-22T03:26:00.000000000'],dtype="datetime64[ns]")
print(np.where(np.in1d(Setups,newdates)))

output:

(array([0, 4]),)
Sign up to request clarification or add additional context in comments.

1 Comment

hello there I have another question that is very similar to this i would appreciate it if you could take a look: stackoverflow.com/questions/68554897/…

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.