Skip to content
Advertisement

Pandas select dataframe rows between multiple date times

Current df:

JavaScript

I have the df with Date and a float number. Date is the index and is unique. I would like to create a new df based on the dates found in the next df.

JavaScript

I expect to get:

JavaScript

In other word I want to filter the initial df and find all rows between all the dates found in the second df.

I thought of using pandas.DataFrame.between_time. But the issue is this works only for 1 given date start and date end. How can I do this with many different date periods?

Advertisement

Answer

Use np.logical_or.reduce with list comprehension:

JavaScript

If DatetimeIndex is possible use:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement