Skip to content

Commit 9154732

Browse files
author
Christoph Paulik
committed
BUG: Fix length 1 .loc slicing with datetime string GH16071
1 parent b17e286 commit 9154732

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,7 @@ Conversion
15751575
- 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`)
15761576
- Bug in the return type of ``pd.unique`` on a ``Categorical``, which was returning an ndarray and not a ``Categorical`` (:issue:`15903`)
15771577
- Bug in ``Index.to_series()`` where the index was not copied (and so mutating later would change the original), (:issue:`15949`)
1578+
- Bug in indexing with partial string indexing with a len-1 DataFrame (:issue:`16071`)
15781579

15791580
Indexing
15801581
^^^^^^^^

pandas/core/indexes/datetimes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,8 +1470,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
14701470
# [parsed, parsed + 1 freq)
14711471
# because label may be passed to searchsorted
14721472
# the bounds need swapped if index is reverse sorted and has a
1473-
# length (is_monotonic_decreasing gives True for empty index)
1474-
if self.is_monotonic_decreasing and len(self):
1473+
# length > 1 (is_monotonic_decreasing gives True for empty
1474+
# and length 1 index)
1475+
if self.is_monotonic_decreasing and len(self) > 1:
14751476
return upper if side == 'left' else lower
14761477
return lower if side == 'left' else upper
14771478
else:

pandas/tests/indexes/datetimes/test_partial_slcing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,16 @@ def test_partial_slice_doesnt_require_monotonicity(self):
254254
self.assertRaisesRegexp(KeyError,
255255
r"Timestamp\('2014-01-10 00:00:00'\)",
256256
lambda: nonmonotonic.loc[timestamp:])
257+
258+
def test_loc_datetime_length_one(self):
259+
"""
260+
GH16071
261+
"""
262+
df = pd.DataFrame(columns=['1'],
263+
index=pd.date_range('2016-10-01T00:00:00',
264+
'2016-10-01T23:59:59'))
265+
result = df.loc[datetime(2016, 10, 1):]
266+
tm.assert_frame_equal(result, df)
267+
268+
result = df.loc['2016-10-01T00:00:00':]
269+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)