Skip to content

BUG: _apply_rule() ignores tz info on an empty list of observances #54655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 22, 2023
19 changes: 19 additions & 0 deletions pandas/tests/tseries/holiday/test_holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
HolidayCalendarFactory,
Timestamp,
USColumbusDay,
USFederalHolidayCalendar,
USLaborDay,
USMartinLutherKingJr,
USMemorialDay,
Expand Down Expand Up @@ -311,3 +312,21 @@ class TestHolidayCalendar(AbstractHolidayCalendar):
tm.assert_index_equal(date_interval_low, expected_results)
tm.assert_index_equal(date_window_edge, expected_results)
tm.assert_index_equal(date_interval_high, expected_results)


def test_holidays_with_timezone_specified_but_no_occurences():
# GH 54580
# _apply_rule() in holiday.py was silently dropping timezones if you passed it
# an empty list of holiday dates that had timezone information
USFederalHolidayCalendar()
start_date = Timestamp("2018-01-01", tz="America/Chicago")
end_date = Timestamp("2018-01-11", tz="America/Chicago")
expected_results = DatetimeIndex(
["2018-01-01 00:00:00-06:00"],
dtype="datetime64[ns, America/Chicago]",
freq=None,
)

tm.assert_equal(
USFederalHolidayCalendar().holidays(start_date, end_date), expected_results
)
4 changes: 3 additions & 1 deletion pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ def _apply_rule(self, dates: DatetimeIndex) -> DatetimeIndex:
Dates with rules applied
"""
if dates.empty:
return DatetimeIndex([])
# Handle case where we get an empty series of dates with a
# defined timezone
return DatetimeIndex([]).tz_localize(dates.tz)

if self.observance is not None:
return dates.map(lambda d: self.observance(d))
Expand Down