0

How can I write a code that shows me the indexes of Newdate within Setups. I want to get the index value for each of the date values, so for the first output;'2017-12-22T03:31:00.000000000' date value in Newdate comes 6th from the beginning from Setups the output will be 5.How would I be able to get the Expected Output.

Code:

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]")

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

Expected Output:

[0, 5, 1, 6]

2 Answers 2

2

You could convert setups to a list and use index():

setups_list = list(Setups)
indices = [setups_list.index(n) for n in Newdate]
print(indices)

# [0, 5, 1, 6]
Sign up to request clarification or add additional context in comments.

Comments

0

You can use numpy.isin to get the truthfulness of the values i.e. either the values are common or not, then pass it to numpy.where which will give you the indices of True values

>>> np.where(np.isin(Setups, Newdate))
(array([0, 1, 5, 6], dtype=int64),)

2 Comments

This returns the position of items present in the Setups but not in order they are given in Newdate. For example if you had recurring items in Newdate , this solution doesn't provide their indices
@OluwafemiSule, yeah that is True that it returns the position based on the values in Setups but not in Newdate

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.