Nice to meet you, everyone. My name is @tkmbn and I am a GIS engineer at Optimind Co., Ltd.. This is the first post to Qiita.
I will explain about the time frame (combination of date and time, start date and time to end date and time) and how to use the library time-window that does this.
It organizes complicated time frames neatly! ← What I want to do most with this library.
When acquiring data with a time frame from the DB, there was a request to summarize if there is an overlap in the time frame. I tried to implement it by myself somehow, but I got confused and was looking for "I wonder if there is any good tool ..." Library above / time-window) I met. In addition to the readme, there were various other useful functions, so I wrote an article including a memorandum for myself.
Full commentary! That's not the case, so I don't know. .. ..
pip install time-window
Please think that the first one is a Japanese translation of the readme lol
When you want to create a TimeWindow from March 01, 2020 12:00:00
to
March 01, 2020 15:00:00`. .. ..
from datetime import datetime
from time_window import TimeWindow
t1 = datetime(2020, 3, 1, 12, 00, 00)
t2 = datetime(2020, 3, 1, 15, 00, 00)
tw1 = TimewWindow(t1, t2)
tw1
-> TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0))
At this time, note that the time frame will be [t1, t2)
!
It can also be created with timedelta.
from datetime import datetime, timedelta
from time_window import TimeWindow
t1 = datetime(2020, 3, 1, 12, 00, 00)
delta = timedelta(hours=3)
tw2 = TimeWindow.from_timedelta(t1, delta)
tw2
-> TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0))
The same result was obtained.
delta, middle If you want to know the difference of the time frame,
tw1.delta
-> datetime.timedelta(0, 10800)
If you want to know the point in the middle of the time frame,
tw1.middle
-> datetime.datetime(2020, 3, 1, 13, 30)
overlaps
tw = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
tw2 = TimeWindow(datetime(2020, 3, 1, 13, 0), datetime(2020, 3, 1, 18, 0))
tw.overlaps(tw2)
-> True
True if they overlap, False otherwise.
tw = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
tw4 = TimeWindow(datetime(2020, 3, 1, 13, 0), datetime(2020, 3, 1, 14, 0))
tw.contains(tw4)
-> True
True if included, False otherwise.
contiguous
tw = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
tw3 = TimeWindow(datetime(2020, 3, 1, 15, 0), datetime(2020, 3, 1, 18, 0))
tw.contiguous(tw3)
-> [TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 15, 0), datetime.datetime(2020, 3, 1, 18, 0))]
If it touches, the list of TimeWindow is returned, and if it does not touch, False is returned.
tw = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
tw2 = TimeWindow(datetime(2020, 3, 1, 13, 0), datetime(2020, 3, 1, 18, 0))
tw.intersection(tw2)
-> TimeWindow(datetime.datetime(2020, 3, 1, 13, 0), datetime.datetime(2020, 3, 1, 15, 0))
tw.union(tw2)
-> TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 18, 0))
from datetime import datetime, timedelta
from time_window import TimeWindow, TimeWindowsCollection
tw = TimeWindow(datetime(2020, 3, 1, 13, 0), datetime(2020, 3, 1, 18, 0))
tw2 = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
twc = TimeWindowsCollection([tw, tw2])
twc
-> [TimeWindow(datetime.datetime(2020, 3, 1, 13, 0), datetime.datetime(2020, 3, 1, 18, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0))]
##I want to sort by start time!
twc.time_windows_sorted_by_since
-> [TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 13, 0), datetime.datetime(2020, 3, 1, 18, 0))]
Now I've done exactly what I want to do.
tw = TimeWindow(datetime(2020, 3, 1, 13, 0), datetime(2020, 3, 1, 18, 0))
tw2 = TimeWindow(datetime(2020, 3, 1, 12, 0), datetime(2020, 3, 1, 15, 0))
tw3 = TimeWindow(datetime(2020, 3, 1, 19, 0), datetime(2020, 3, 1, 21, 0))
twc = TimeWindowsCollection([tw, tw2, tw3])
twc
-> [TimeWindow(datetime.datetime(2020, 3, 1, 13, 0), datetime.datetime(2020, 3, 1, 18, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 15, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 19, 0), datetime.datetime(2020, 3, 1, 21, 0))]
twc.compressed()
-> [TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 18, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 19, 0), datetime.datetime(2020, 3, 1, 21, 0))]
The overlapping time frames are grouped together, and the non-overlapping time frames are different time frames.
However, there was a pitfall. When I tried to retrieve the TimeWindow by specifying the index normally,
twc.compressed()[0]
-> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'TimeWindowsCollection' object does not support indexing
Looking at the returned form, it looks like a normal list, but it seems that it is different. .. .. Since we need to move the TimeWindowsCollection object back to the TimeWindow list,
twc.compressed().time_windows
-> [TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 18, 0)),
TimeWindow(datetime.datetime(2020, 3, 1, 19, 0), datetime.datetime(2020, 3, 1, 21, 0))]
twc.compressed().time_windows[0]
TimeWindow(datetime.datetime(2020, 3, 1, 12, 0), datetime.datetime(2020, 3, 1, 18, 0))
I was able to take it out safely! !!
Thank you for reading to the end! If you have any questions or mistakes, please comment.
Recommended Posts