Skip to content

Commit 8dd045d

Browse files
BUG: _apply_rule() ignores tz info on an empty list of observances (#54655)
* _apply_rule() instantiates a new empty DatetimeIndex without regard to possible tz in an empty series of provided dates. * Added a test. * Let's format this. * Update pandas/tseries/holiday.py Co-authored-by: Matthew Roeschke <[email protected]> * Wrapping up post-comment changes: delete superflous object, and clean up the test. * Let's put that back. * Update whatsnew * Moved update to correct version file under doc/source/whatsnew * Revert commit. * Fix wording on the whatsnew update. * Update doc/source/whatsnew/v2.2.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * Update doc/source/whatsnew/v2.2.0.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 487c594 commit 8dd045d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Performance improvements
120120

121121
Bug fixes
122122
~~~~~~~~~
123+
- Bug in :class:`AbstractHolidayCalendar` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
123124

124125
Categorical
125126
^^^^^^^^^^^

pandas/tests/tseries/holiday/test_holiday.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import pytest
44
from pytz import utc
55

6-
from pandas import DatetimeIndex
6+
from pandas import (
7+
DatetimeIndex,
8+
Series,
9+
)
710
import pandas._testing as tm
811

912
from pandas.tseries.holiday import (
@@ -17,6 +20,7 @@
1720
HolidayCalendarFactory,
1821
Timestamp,
1922
USColumbusDay,
23+
USFederalHolidayCalendar,
2024
USLaborDay,
2125
USMartinLutherKingJr,
2226
USMemorialDay,
@@ -311,3 +315,17 @@ class TestHolidayCalendar(AbstractHolidayCalendar):
311315
tm.assert_index_equal(date_interval_low, expected_results)
312316
tm.assert_index_equal(date_window_edge, expected_results)
313317
tm.assert_index_equal(date_interval_high, expected_results)
318+
319+
320+
def test_holidays_with_timezone_specified_but_no_occurences():
321+
# GH 54580
322+
# _apply_rule() in holiday.py was silently dropping timezones if you passed it
323+
# an empty list of holiday dates that had timezone information
324+
start_date = Timestamp("2018-01-01", tz="America/Chicago")
325+
end_date = Timestamp("2018-01-11", tz="America/Chicago")
326+
test_case = USFederalHolidayCalendar().holidays(
327+
start_date, end_date, return_name=True
328+
)
329+
expected_results = Series("New Year's Day", index=[start_date])
330+
331+
tm.assert_equal(test_case, expected_results)

pandas/tseries/holiday.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def _apply_rule(self, dates: DatetimeIndex) -> DatetimeIndex:
354354
Dates with rules applied
355355
"""
356356
if dates.empty:
357-
return DatetimeIndex([])
357+
return dates.copy()
358358

359359
if self.observance is not None:
360360
return dates.map(lambda d: self.observance(d))

0 commit comments

Comments
 (0)