Last active
October 9, 2021 07:50
-
-
Save andrea-mucci/9abfcfb181b74ce140e1250de4df5d19 to your computer and use it in GitHub Desktop.
create a timeseries for a reservation calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from datetime import datetime | |
def get_availabilities(start, end, freq, weekmask=None, bhours=None, bstop=None, holidays=None): | |
""" | |
:param start: datetime | |
:param end: datetime | |
:param freq: int | |
:param weekmask: list optional | |
:param bhours: list optional | |
:param bstop: list optional | |
:param holidays: list optional | |
:return: | |
example | |
# we want a calendar from the 1-10-2021 until 31-10-2021 | |
start = datetime(2021, 10, 1, 0, 0) | |
end = datetime(2021, 10, 31, 0, 0) | |
# the store is open during the following days | |
weekmask = 'Monday Tuesday Wednesday Thursday Friday' | |
# the store business hours are from 09:30 until 19:30 | |
bday = ["09:30", "19:30"] | |
# the store close during the day from 14:30 until 15:30, for example during the "Spanish Lunch Time" | |
bstop = ["14:30", "15:30"] | |
# the store is in holidays the days 1-10-2021 and 4-10-2021 | |
holidays = [datetime(2021, 10, 1).date(), datetime(2021, 10, 4).date()] | |
# get the DataFrame with the list of the Date and Time every 15 minutes from 1-10-2021 until 31-10-2021 | |
dataset = get_availabilities(start, end, 15, weekmask, bday, bstop, holidays) | |
print(dataset) | |
""" | |
week_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
freq = f"{freq}T" | |
serie = pd.date_range(start=start, end=end, freq=freq) | |
# convert to a DataFrame | |
df = serie.to_frame(index=False, name="availabilities") | |
# get Week Days column | |
if weekmask is not None: | |
weekmask_list = weekmask.split(" ") | |
df['day_of_week'] = df['availabilities'].dt.day_name() | |
# apply the weekmask | |
days_stop = list(set(week_days) ^ set(weekmask_list)) | |
df = df[~df["day_of_week"].isin(days_stop)] | |
df.drop("day_of_week", axis=1, inplace=True) | |
if holidays is not None and isinstance(holidays, list): | |
# define the business stop during the day | |
df = df[~df['availabilities'].dt.date.isin(holidays)] | |
if bhours is not None and isinstance(bhours, list): | |
# define the business hours | |
df = df[(df['availabilities'].dt.time >= datetime.strptime(bhours[0], '%H:%M').time()) | |
& (df['availabilities'].dt.time <= datetime.strptime(bhours[1], '%H:%M').time())] | |
if bstop is not None and isinstance(bstop, list): | |
# define the business stop during the day | |
df = df[(df['availabilities'].dt.time < datetime.strptime(bstop[0], '%H:%M').time()) | |
| (df['availabilities'].dt.time > datetime.strptime(bstop[1], '%H:%M').time())] | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment