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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ Other
- Bug in :meth:`Series.argsort` failing to raise when an invalid ``axis`` is passed (:issue:`54257`)
- Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`)
- Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`)
- Bug in :meth:`apply_rule` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
- Bug in :meth:`period_range` the default behavior when freq was not passed as an argument was incorrect(:issue:`53687`)
- Fixed incorrect ``__name__`` attribute of ``pandas._libs.json`` (:issue:`52898`)

Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/tseries/holiday/test_holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import pytest
from pytz import utc

from pandas import DatetimeIndex
from pandas import (
DatetimeIndex,
Series,
)
import pandas._testing as tm

from pandas.tseries.holiday import (
Expand All @@ -17,6 +20,7 @@
HolidayCalendarFactory,
Timestamp,
USColumbusDay,
USFederalHolidayCalendar,
USLaborDay,
USMartinLutherKingJr,
USMemorialDay,
Expand Down Expand Up @@ -311,3 +315,17 @@ 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
start_date = Timestamp("2018-01-01", tz="America/Chicago")
end_date = Timestamp("2018-01-11", tz="America/Chicago")
test_case = USFederalHolidayCalendar().holidays(
start_date, end_date, return_name=True
)
expected_results = Series("New Year's Day", index=[start_date])

tm.assert_equal(test_case, expected_results)
2 changes: 1 addition & 1 deletion pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _apply_rule(self, dates: DatetimeIndex) -> DatetimeIndex:
Dates with rules applied
"""
if dates.empty:
return DatetimeIndex([])
return dates.copy()

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