Skip to content

Commit 37bf762

Browse files
jrmylowpmhatre1
authored andcommitted
BUG fix for date_range 56134 (pandas-dev#56831)
1 parent d4b08d5 commit 37bf762

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

doc/source/whatsnew/v2.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Categorical
118118

119119
Datetimelike
120120
^^^^^^^^^^^^
121-
-
121+
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
122122
-
123123

124124
Timedelta

pandas/core/arrays/datetimes.py

-5
Original file line numberDiff line numberDiff line change
@@ -2775,11 +2775,6 @@ def _generate_range(
27752775
# variable has type "Optional[Timestamp]")
27762776
start = offset.rollforward(start) # type: ignore[assignment]
27772777

2778-
elif end and not offset.is_on_offset(end):
2779-
# Incompatible types in assignment (expression has type "datetime",
2780-
# variable has type "Optional[Timestamp]")
2781-
end = offset.rollback(end) # type: ignore[assignment]
2782-
27832778
# Unsupported operand types for < ("Timestamp" and "None")
27842779
if periods is None and end < start and offset.n >= 0: # type: ignore[operator]
27852780
end = None

pandas/tests/indexes/datetimes/test_date_range.py

+15
Original file line numberDiff line numberDiff line change
@@ -1703,3 +1703,18 @@ def test_date_range_freqstr_matches_offset(self, freqstr, offset):
17031703
idx2 = date_range(start=sdate, end=edate, freq=offset)
17041704
assert len(idx1) == len(idx2)
17051705
assert idx1.freq == idx2.freq
1706+
1707+
def test_date_range_partial_day_year_end(self, unit):
1708+
# GH#56134
1709+
rng = date_range(
1710+
start="2021-12-31 00:00:01",
1711+
end="2023-10-31 00:00:00",
1712+
freq="YE",
1713+
unit=unit,
1714+
)
1715+
exp = DatetimeIndex(
1716+
["2021-12-31 00:00:01", "2022-12-31 00:00:01"],
1717+
dtype=f"M8[{unit}]",
1718+
freq="YE",
1719+
)
1720+
tm.assert_index_equal(rng, exp)

0 commit comments

Comments
 (0)