diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 7b53ddb3923f0..75f68e1bd0a48 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -119,7 +119,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ -- +- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`) - Timedelta diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index a4d01dd6667f6..8a9b19dc7934f 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2775,11 +2775,6 @@ def _generate_range( # variable has type "Optional[Timestamp]") start = offset.rollforward(start) # type: ignore[assignment] - elif end and not offset.is_on_offset(end): - # Incompatible types in assignment (expression has type "datetime", - # variable has type "Optional[Timestamp]") - end = offset.rollback(end) # type: ignore[assignment] - # Unsupported operand types for < ("Timestamp" and "None") if periods is None and end < start and offset.n >= 0: # type: ignore[operator] end = None diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 024f37ee5b710..ec158f7b194a0 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -1703,3 +1703,18 @@ def test_date_range_freqstr_matches_offset(self, freqstr, offset): idx2 = date_range(start=sdate, end=edate, freq=offset) assert len(idx1) == len(idx2) assert idx1.freq == idx2.freq + + def test_date_range_partial_day_year_end(self, unit): + # GH#56134 + rng = date_range( + start="2021-12-31 00:00:01", + end="2023-10-31 00:00:00", + freq="YE", + unit=unit, + ) + exp = DatetimeIndex( + ["2021-12-31 00:00:01", "2022-12-31 00:00:01"], + dtype=f"M8[{unit}]", + freq="YE", + ) + tm.assert_index_equal(rng, exp)