This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch v2-6-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit b248c174348678553da6f9fe6d94fdde716a4b21
Author: Jarek Potiuk <[email protected]>
AuthorDate: Sat Apr 15 14:49:26 2023 +0200

    Make pandas optional in workday calendar example (#30660)
    
    The workday calendar expected pandas to be available and it is part
    of our examples, however Airflow does not have pandas as a core
    dependency, so in case someone does not have pandas installed, importing
    of the workday example would fail.
    
    This change makes pandas optional and fallbacks to regular working
    days for the example in case it is not available (including warning
    about it). It also fixes a slight inefficiency where the
    USFederalHoliday calendar has been created every time next workday
    was calculated.
    
    (cherry picked from commit 5b42aa3b8d0ec069683e22c2cb3b8e8e6e5fee1c)
---
 airflow/example_dags/plugins/workday.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/airflow/example_dags/plugins/workday.py 
b/airflow/example_dags/plugins/workday.py
index 92368de0ae..5dc3f2bc50 100644
--- a/airflow/example_dags/plugins/workday.py
+++ b/airflow/example_dags/plugins/workday.py
@@ -18,28 +18,38 @@
 """Plugin to demonstrate timetable registration and accommodate example 
DAGs."""
 from __future__ import annotations
 
+import logging
 from datetime import timedelta
 
 # [START howto_timetable]
-from pandas.tseries.holiday import USFederalHolidayCalendar
 from pendulum import UTC, Date, DateTime, Time
 
 from airflow.plugins_manager import AirflowPlugin
 from airflow.timetables.base import DagRunInfo, DataInterval, TimeRestriction, 
Timetable
 
+log = logging.getLogger(__name__)
+holiday_calendar = None
+
+try:
+    from pandas.tseries.holiday import USFederalHolidayCalendar
+
+    holiday_calendar = USFederalHolidayCalendar()
+except ImportError:
+    log.warning("Could not import pandas. Holidays will not be considered.")
+
 
 class AfterWorkdayTimetable(Timetable):
     def get_next_workday(self, d: DateTime, incr=1) -> DateTime:
-        cal = USFederalHolidayCalendar()
         next_start = d
         while True:
             if next_start.weekday() in (5, 6):  # If next start is in the 
weekend go to next day
                 next_start = next_start + incr * timedelta(days=1)
                 continue
-            holidays = cal.holidays(start=next_start, 
end=next_start).to_pydatetime()
-            if next_start in holidays:  # If next start is a holiday go to 
next day
-                next_start = next_start + incr * timedelta(days=1)
-                continue
+            if holiday_calendar is not None:
+                holidays = holiday_calendar.holidays(start=next_start, 
end=next_start).to_pydatetime()
+                if next_start in holidays:  # If next start is a holiday go to 
next day
+                    next_start = next_start + incr * timedelta(days=1)
+                    continue
             break
         return next_start
 

Reply via email to