From 8abb7351635932a02d750895167a93631c809ca7 Mon Sep 17 00:00:00 2001 From: Douglas Lohmann Date: Thu, 10 Nov 2022 17:36:39 -0300 Subject: [PATCH] BUG: date_range with freq="C" (business days) return value changed on 1.5.0 (#49610) BUG: Use naive wall time to perform offsets datetime64 conversion --- doc/source/whatsnew/v1.5.2.rst | 1 + pandas/_libs/tslibs/offsets.pyx | 4 +++- .../tests/indexes/datetimes/test_date_range.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index 572d6c74e767f4..446235d1656dc2 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -17,6 +17,7 @@ Fixed regressions - Fixed regression in :meth:`Series.replace` raising ``RecursionError`` with numeric dtype and when specifying ``value=None`` (:issue:`45725`) - Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`) +- Fixed regression in :func:`date_range` returning an invalid set of periods for ``CustomBusinessDay`` frequency and ``start`` date with timezone (:issue:`49441`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 50d6a0a02b0cfd..8e022ac662d217 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -254,7 +254,9 @@ cdef _to_dt64D(dt): if getattr(dt, 'tzinfo', None) is not None: # Get the nanosecond timestamp, # equiv `Timestamp(dt).value` or `dt.timestamp() * 10**9` - naive = dt.astimezone(None) + # The `naive` must be the `dt` naive wall time + # instead of the naive absolute time (GH#49441) + naive = dt.replace(tzinfo=None) dt = np.datetime64(naive, "D") else: dt = np.datetime64(dt) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 142679e292b38a..adbf6c715fef65 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -1151,6 +1151,24 @@ def test_range_with_millisecond_resolution(self, start_end): expected = DatetimeIndex([start]) tm.assert_index_equal(result, expected) + @pytest.mark.parametrize( + "start,period,expected", + [ + ("2022-07-23 00:00:00+02:00", 1, ["2022-07-25 00:00:00+02:00"]), + ("2022-07-22 00:00:00+02:00", 1, ["2022-07-22 00:00:00+02:00"]), + ( + "2022-07-22 00:00:00+02:00", + 2, + ["2022-07-22 00:00:00+02:00", "2022-07-25 00:00:00+02:00"], + ), + ], + ) + def test_range_with_timezone_and_custombusinessday(self, start, period, expected): + # GH49441 + result = date_range(start=start, periods=period, freq="C") + expected = DatetimeIndex(expected) + tm.assert_index_equal(result, expected) + def test_date_range_with_custom_holidays(): # GH 30593