Skip to content

REGR: Fixed slicing DatetimeIndex with date #31521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Interval

Indexing
^^^^^^^^

- Fixed regression when indexing a ``Series`` or ``DataFrame`` indexed by ``DatetimeIndex`` with a slice containg a :class:`datetime.date` (:issue:`31501`)
- Fixed regression in :class:`DataFrame` setting values with a slice (e.g. ``df[-4:] = 1``) indexing by label instead of position (:issue:`31469`)
-
-
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, time, timedelta, tzinfo
from datetime import date, datetime, time, timedelta, tzinfo
import operator
from typing import Optional
import warnings
Expand Down Expand Up @@ -758,6 +758,13 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
if isinstance(start, time) or isinstance(end, time):
raise KeyError("Cannot mix time and non-time slice keys")

# Pandas supports slicing with dates, treated as datetimes at midnight.
# https://github.com/pandas-dev/pandas/issues/31501
if isinstance(start, date) and not isinstance(start, datetime):
start = datetime.combine(start, time(0, 0))
if isinstance(end, date) and not isinstance(end, datetime):
end = datetime.combine(end, time(0, 0))

try:
return Index.slice_indexer(self, start, end, step, kind=kind)
except KeyError:
Expand Down
22 changes: 21 additions & 1 deletion pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta

from dateutil import tz
import numpy as np
Expand Down Expand Up @@ -350,3 +350,23 @@ def test_loc_label_slicing(self):
expected = ser.iloc[:-1]

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"slice_, positions",
[
[slice(date(2018, 1, 1), None), [0, 1, 2]],
[slice(date(2019, 1, 2), None), [2]],
[slice(date(2020, 1, 1), None), []],
[slice(None, date(2020, 1, 1)), [0, 1, 2]],
[slice(None, date(2019, 1, 1)), [0]],
],
)
def test_getitem_slice_date(self, slice_, positions):
# https://github.com/pandas-dev/pandas/issues/31501
s = pd.Series(
[0, 1, 2],
pd.DatetimeIndex(["2019-01-01", "2019-01-01T06:00:00", "2019-01-02"]),
)
result = s[slice_]
expected = s.take(positions)
tm.assert_series_equal(result, expected)