I have a pandas datetime index idx
with with minute frequency, I also have a list(or set) of dates list_of_dates
. I would like to return a boolean array of the same size of idx
with the condition that the dates of datetime index belong is in the list_of_dates
. Is it possible to do it in a vectorized way (i.e. not using a for loop)?
Asked
Active
Viewed 1,255 times
0

motam79
- 3,542
- 5
- 34
- 60
-
use `isin()` method so where you stucked? – Anurag Dabas Jul 22 '21 at 12:41
-
try `df['flag'] = df.index.isin(l)` – Da Song Jul 22 '21 at 12:41
-
Does this answer your question? [Use a list of values to select rows from a Pandas dataframe](https://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe) – Anurag Dabas Jul 22 '21 at 12:56
1 Answers
1
Since you're trying to compare only the dates, you could remove the times and compare like so:
>>> df.index.normalize().isin(list_of_dates)
Or:
>>> df.index.floor('D').isin(list_of_dates)

not_speshal
- 22,093
- 2
- 15
- 30