how to use intervalindex with datetimes?
See original GitHub issueHello there!
I am struggling with the amazing intervalindex
new feature.
Consider this simple example:
df = pd.DataFrame({'id' : ['pandas','peanuts'],
'inter_left' : [pd.to_datetime('2018-08-01 10:00:00'),
pd.to_datetime('2018-08-01 17:00:00')],
'inter_right' : [pd.to_datetime('2018-08-01 12:00:00'),
pd.to_datetime('2018-08-01 18:00:00')]})
Out[81]:
id inter_left inter_right
0 pandas 2018-08-01 10:00:00 2018-08-01 12:00:00
1 peanuts 2018-08-01 17:00:00 2018-08-01 18:00:00
df_other = pd.DataFrame({'id' : ['jeff','wes'],
'inter_left' : [pd.to_datetime('2018-08-01 11:00:00'),
pd.to_datetime('2018-08-01 20:00:00')],
'inter_right' : [pd.to_datetime('2018-08-01 13:00:00'),
pd.to_datetime('2018-08-01 22:00:00')]})
Out[85]:
id inter_left inter_right
0 jeff 2018-08-01 11:00:00 2018-08-01 13:00:00
1 wes 2018-08-01 20:00:00 2018-08-01 22:00:00
Here, each row in df
identifies an individual, and inter_left
and inter_right
are the left and right boundaries of the interval at which id
is observed.
I would like to ‘merge’ this dataframe with df_other
with the understanding that an id from df_other
is merged to an id
from df
only if its corresponding interval overlaps the one of id
.
In the example, only jeff
, who is there between 11 and 13, overlaps with pandas
, who is there between 10 and 12.
My understanding is that intervalindex
can do that. Despite my attempts, I wasnt able to make it work. Is this supposed to be doable?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Pandas : Datetimeindex and Intervalindex intersection
1) work on timestamps instead of datetimes. 2) work with numpy arrays instead of dataframes. 3) do this simultaneous iterating over events ...
Read more >pandas.IntervalIndex — pandas 1.5.2 documentation
Immutable index of intervals that are closed on the same side. New in version 0.20.0. ... See the user guide for more. ......
Read more >Pandas: Create an Intervals index - w3resource
Write a Pandas program to create a DataFrame using intervals as an index. IntervalIndex represents an Index of Interval objects that are all ......
Read more >How do I make an unbounded time interval index with pandas?
In this example, I put 2020 as the end date, but in reality, I don't know the end date, and I don't want...
Read more >MATLAB isbetween - MathWorks
Determine elements within date and time interval ... To display the dates that lie within the interval, index into t using tf as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://stackoverflow.com/questions/46525786/how-to-join-two-dataframes-for-which-column-values-are-within-a-certain-range/46526249
I guess this is solved