question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to use intervalindex with datetimes?

See original GitHub issue

Hello 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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found