Skip to content

Commit c8018b5

Browse files
Backport PR pandas-dev#49610 on branch 1.5.x (BUG: date_range with freq="C" (business days) return value changed on 1.5.0) (pandas-dev#49625)
Backport PR pandas-dev#49610: BUG: date_range with freq="C" (business days) return value changed on 1.5.0 Co-authored-by: Douglas Lohmann <[email protected]>
1 parent 0aab994 commit c8018b5

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v1.5.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
- Fixed regression in :meth:`Series.replace` raising ``RecursionError`` with numeric dtype and when specifying ``value=None`` (:issue:`45725`)
1717
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
1818
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)
19+
- Fixed regression in :func:`date_range` returning an invalid set of periods for ``CustomBusinessDay`` frequency and ``start`` date with timezone (:issue:`49441`)
1920
-
2021

2122
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/offsets.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ cdef _to_dt64D(dt):
258258
if getattr(dt, 'tzinfo', None) is not None:
259259
# Get the nanosecond timestamp,
260260
# equiv `Timestamp(dt).value` or `dt.timestamp() * 10**9`
261-
naive = dt.astimezone(None)
261+
# The `naive` must be the `dt` naive wall time
262+
# instead of the naive absolute time (GH#49441)
263+
naive = dt.replace(tzinfo=None)
262264
dt = np.datetime64(naive, "D")
263265
else:
264266
dt = np.datetime64(dt)

pandas/tests/indexes/datetimes/test_date_range.py

+18
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,24 @@ def test_range_with_millisecond_resolution(self, start_end):
11261126
expected = DatetimeIndex([start])
11271127
tm.assert_index_equal(result, expected)
11281128

1129+
@pytest.mark.parametrize(
1130+
"start,period,expected",
1131+
[
1132+
("2022-07-23 00:00:00+02:00", 1, ["2022-07-25 00:00:00+02:00"]),
1133+
("2022-07-22 00:00:00+02:00", 1, ["2022-07-22 00:00:00+02:00"]),
1134+
(
1135+
"2022-07-22 00:00:00+02:00",
1136+
2,
1137+
["2022-07-22 00:00:00+02:00", "2022-07-25 00:00:00+02:00"],
1138+
),
1139+
],
1140+
)
1141+
def test_range_with_timezone_and_custombusinessday(self, start, period, expected):
1142+
# GH49441
1143+
result = date_range(start=start, periods=period, freq="C")
1144+
expected = DatetimeIndex(expected)
1145+
tm.assert_index_equal(result, expected)
1146+
11291147

11301148
def test_date_range_with_custom_holidays():
11311149
# GH 30593

0 commit comments

Comments
 (0)