Skip to content Skip to sidebar Skip to footer

Is It Possible To Use Pandas Overlap In A Dataframe?

Python 3.7, Pandas 25 I have a Pandas Dataframe with columns for startdate and enddate. I am looking for ranges that overlap the range of my variable(s). Without being verbose an

Solution 1:

You need to create IntervalIndex from df.startdate and df.enddate and use overlaps against reporttest_range. Your sample returns all true, so I add row for False case.

Sample df:recordstartdateenddate09931 2017-07-01 2018-06-301280752018-08-01 2021-07-312100422017-07-01 2018-06-303281082017-07-01 2018-06-304281092016-07-01 2016-12-305281112017-07-02 2018-09-30iix=pd.IntervalIndex.from_arrays(df.startdate,df.enddate,closed='both')iix.overlaps(reporttest_range)Out[400]:array([True,True,True,True,False,True])

Use it to pick only overlapping rows

df[iix.overlaps(reporttest_range)]Out[401]:recordstartdateenddate09931 2017-07-01 2018-06-301280752018-08-01 2021-07-312100422017-07-01 2018-06-303281082017-07-01 2018-06-305281112017-07-02 2018-09-30

Post a Comment for "Is It Possible To Use Pandas Overlap In A Dataframe?"