Reputation: 1345
Assume that I have the following data
import numpy as np, pandas as pd
numids = 3
start, end = datetime.datetime(2015, 1, 1), datetime.datetime(2016, 2, 15)
date_list = np.tile(pd.date_range(start, end, freq='B'), numids)
How do I find all indices where , say, date_list == datetime.datetime(2015, 3, 31)
? This should be an easy problem, but I can't seem to figure it out. Thanks, Tingis
Edit
If we take a simple example and set numids
to 1
and the date of interest to be datetime.datetime(2015, 1, 1)
, I would like to get the index 1
. Or a list [True, False, False, ..., False]
.
Edit 2 Now, I try to just loop through the array and calculate the difference in days, such as
for date in date_list:
print(date - datetime.datetime(2015, 1, 12))
However, this gives me an error that I've never seen
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')
Upvotes: 1
Views: 7650
Reputation: 90989
You can try this -
np.where(date_list == np.datetime64(datetime.datetime(2015, 3, 31)))
>>> (array([ 63, 356, 649], dtype=int64),)
Upvotes: 3