Setting up a scenario to run on 5th business day of the month. Check if code will work properly.
![jrmathieu63](https://us.v-cdn.net/6038231/uploads/Dataiku/nAvatar4.png)
I have need to start a scenario to run on 5th business (work day).
I have the following python for the custom trigger and just want to be sure this will work for all months.
import pandas as pd from datetime import datetime #get today's date in YYYY-MM-DD format todaystr = datetime.today().strftime("%Y-%m-%d") s = pd.date_range(todaystr, periods=31, freq='B') df = pd.DataFrame(s, columns=['Date']) # get the 5th business day of the current month from the dataframe fifthBDCMstr = df.iloc[4]['Date'].strftime("%Y-%m-%d") if fifthBDCMstr == todaystr: from dataiku.scenario import Trigger t = Trigger() t.fire()
Just not sure about the date_range since it is defining the max month value.
Would this be an issue for old months?
Operating system used: AL2
Answers
-
Turribeach Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 1,876 Neuron
What I would suggest is that you use pd.date_range() into a for loop and feed your code months for testing and see what the output is. Instead of triggering a scenario just print() something and then you can evaluate your code it's working as expected.
-
Great idea - I will try that - thanks
-
Actually required business days so the previous would not work.
Also need to run for either 5th or 6th day.
Here is the code to determine for either 5th or 6th business day.import pandas as pd
from datetime import datetime
import calendar
# get today's date in YYYY-MM-DD format
todaystr = datetime.today().strftime("%Y-%m-%d")
# get the year and month of today's date
year = datetime.today().year
month = datetime.today().month
_, num_days = calendar.monthrange(year, month)
# need to determine max days for current month
# create a date range with only business days for the current month
s = pd.bdate_range(start=f"{year}-{month}-01", end=f"{year}-{month}-{num_days}")
# get the 5th or 6th business day of the current month as a string in YYYY-MM-DD format
fifthBDCMstr = s[4].strftime("%Y-%m-%d")
sixthBDCMstr = s[5].strftime("%Y-%m-%d")
# check if either of them matches today's date
if fifthBDCMstr == todaystr or sixthBDCMstr == todaystr:
from dataiku.scenario import Trigger
t = Trigger()
t.fire()This code will become true for either 2023-11-07 or 2023-11-08 for next month since 4th and 5th are weekend dates
and 1,2 ,3, 6, 7, 8 are the business dates, which makes 7 and 8 the 5th and 6th business date.