Skip to content

BUG: Fix length 1 .loc slicing with datetime string GH16071 #16072

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

Closed
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,7 @@ Conversion
- Bug in ``is_string_dtype``, ``is_timedelta64_ns_dtype``, and ``is_string_like_dtype`` in which an error was raised when ``None`` was passed in (:issue:`15941`)
- Bug in the return type of ``pd.unique`` on a ``Categorical``, which was returning an ndarray and not a ``Categorical`` (:issue:`15903`)
- Bug in ``Index.to_series()`` where the index was not copied (and so mutating later would change the original), (:issue:`15949`)
- Bug in indexing with partial string indexing with a len-1 DataFrame (:issue:`16071`)

Indexing
^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,8 +1470,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
# [parsed, parsed + 1 freq)
# because label may be passed to searchsorted
# the bounds need swapped if index is reverse sorted and has a
# length (is_monotonic_decreasing gives True for empty index)
if self.is_monotonic_decreasing and len(self):
# length > 1 (is_monotonic_decreasing gives True for empty
# and length 1 index)
if self.is_monotonic_decreasing and len(self) > 1:
return upper if side == 'left' else lower
return lower if side == 'left' else upper
else:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/datetimes/test_partial_slcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,16 @@ def test_partial_slice_doesnt_require_monotonicity(self):
self.assertRaisesRegexp(KeyError,
r"Timestamp\('2014-01-10 00:00:00'\)",
lambda: nonmonotonic.loc[timestamp:])

def test_loc_datetime_length_one(self):
"""
GH16071
"""
df = pd.DataFrame(columns=['1'],
index=pd.date_range('2016-10-01T00:00:00',
'2016-10-01T23:59:59'))
result = df.loc[datetime(2016, 10, 1):]
tm.assert_frame_equal(result, df)

result = df.loc['2016-10-01T00:00:00':]
tm.assert_frame_equal(result, df)