From f45e53b399b954b71c81dec35962aa6f0880eba3 Mon Sep 17 00:00:00 2001 From: lpc Date: Wed, 13 Apr 2022 21:08:19 +0200 Subject: [PATCH 1/3] GH46331: add tests --- .../indexes/datetimes/test_date_range.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 377974a918ad9..965070d31a088 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -783,6 +783,34 @@ def test_range_where_start_equal_end(self, inclusive_endpoints_fixture): tm.assert_index_equal(result, expected) + def test_range_without_start_inclusive(self, inclusive_endpoints_fixture): + # GH 46331 + start = Timestamp("2000-01-01") + end = Timestamp("2000-01-04") + both_range = date_range(start, end, inclusive="both") + + result = date_range(end=end, periods=4, inclusive=inclusive_endpoints_fixture) + expected = _get_expected_range( + start, end, both_range, inclusive_endpoints_fixture + ) + + tm.assert_index_equal(result, expected) + + def test_range_without_end_inclusive(self, inclusive_endpoints_fixture): + # GH 46331 + start = Timestamp("2000-01-01") + end = Timestamp("2000-01-04") + both_range = date_range(start, end, inclusive="both") + + result = date_range( + start=start, periods=4, inclusive=inclusive_endpoints_fixture + ) + expected = _get_expected_range( + start, end, both_range, inclusive_endpoints_fixture + ) + + tm.assert_index_equal(result, expected) + class TestDateRangeTZ: """Tests for date_range with timezones""" From 5660addeca63123c5f60eade3024138977530f29 Mon Sep 17 00:00:00 2001 From: luis pinto Date: Fri, 8 Apr 2022 17:45:54 +0200 Subject: [PATCH 2/3] GH46331: take into account start/end being None --- pandas/core/arrays/datetimes.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 95882b65e9aaa..f8defc23f3b3a 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -484,17 +484,21 @@ def _generate_range( # to overflow and cast to e.g. f8, but if it does we need to cast i8values = i8values.astype("i8") + # Apply `inclusive` rules if start == end: + # Particular case where we act iff `inclusive="neither"` + # cf. GH #43394 if not left_inclusive and not right_inclusive: i8values = i8values[1:-1] - else: + elif len(i8values): start_i8 = Timestamp(start).value end_i8 = Timestamp(end).value - if not left_inclusive or not right_inclusive: - if not left_inclusive and len(i8values) and i8values[0] == start_i8: - i8values = i8values[1:] - if not right_inclusive and len(i8values) and i8values[-1] == end_i8: - i8values = i8values[:-1] + _left, _right = None, None + if not left_inclusive and (i8values[0] == start_i8 or start is None): + _left = 1 + if not right_inclusive and (i8values[-1] == end_i8 or end is None): + _right = -1 + i8values = i8values[_left:_right] dt64_values = i8values.view("datetime64[ns]") dtype = tz_to_dtype(tz) From 484102401c871f3b7b04b3ebc236386b6dd7cd08 Mon Sep 17 00:00:00 2001 From: lpc Date: Wed, 13 Apr 2022 22:38:19 +0200 Subject: [PATCH 3/3] GH46331: whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 358d9447b131d..e96f88d37f52f 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -473,6 +473,7 @@ Datetimelike - Bug in :meth:`Index.astype` when casting from object dtype to ``timedelta64[ns]`` dtype incorrectly casting ``np.datetime64("NaT")`` values to ``np.timedelta64("NaT")`` instead of raising (:issue:`45722`) - Bug in :meth:`SeriesGroupBy.value_counts` index when passing categorical column (:issue:`44324`) - Bug in :meth:`DatetimeIndex.tz_localize` localizing to UTC failing to make a copy of the underlying data (:issue:`46460`) +- Bug in :meth:`date_range` when ``start`` or ``end`` was not specified, failed to exclude first or last date despite ``inclusive`` not being ``"both"`` (:issue:`46331`) - Timedelta