Checking for overlap between time spans python vs. pandas

1,765 views
Skip to first unread message

me

unread,
Jul 10, 2015, 10:43:57 PM7/10/15
to pyd...@googlegroups.com
Hi all

I  am trying to find overlaps between time spans  (start ,end)

I have a python solution and  I am trying to figure ot a simple  equivalent pandas solution
Some idea or hints ?
Regards
g
==IN PYTHON ==
import pandas
intervals = [[100,200],[150,250],[300,400],[250,500]]

overlapping = [ [s,e] for s in intervals for e in intervals if s is not e and s[1]>e[0] and s[0]<e[0] ]

print 'python output'
for x in overlapping:
    print '{0} overlaps with {1}'.format(x[0],x[1])

== USING PANDAS ==
pdi = pandas.DataFrame(intervals)
pdi['overlaps_ix'] = '[?,?]' 
pdi


HINT:
Should i use shift  ? but the i do not have all the combinations 

python output
[100, 200] overlaps with [150, 250]
[250, 500] overlaps with [300, 400]
Out[10]:
01overlaps_ix
0100200[?,?]
1150250[?,?]
2300400[?,?]
3250500[?,?]

Nathaniel Smith

unread,
Jul 10, 2015, 11:14:02 PM7/10/15
to pyd...@googlegroups.com

This doesn't answer your question, but in case the reason you're looking at pandas is speed... a much, much more efficient algorithm (O(n log n) instead of O(n**2)) would be something like:

events = []
for (start, end) in intervals:
    event = (start, end)
    events.append((start, "start", event))
    events.append((end - 0.5, "end", event))
events.sort()

current = set()
overlaps = []
for (_, edge, event) in events:
    if edge == "start":
        for other in current:
            overlaps.append([other, event])
        current.add(event)
    else:
        assert edge == "end"
        current.remove(event)

(This assumes half-open intervals like python range. NB non-tested code typed on a phone, use for entertainment purposes only.)

-n

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages