Skip to content

Commit f7bce69

Browse files
Christoph Paulikjreback
Christoph Paulik
authored andcommitted
BUG: Fix length 1 .loc slicing with datetime string GH16071
closes pandas-dev#16071 Author: Christoph Paulik <[email protected]> Closes pandas-dev#16072 from cpaulik/fix-length-1-loc-slice-GH16071 and squashes the following commits: 9154732 [Christoph Paulik] BUG: Fix length 1 .loc slicing with datetime string GH16071
1 parent 8c7b973 commit f7bce69

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

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

15801581
Indexing
15811582
^^^^^^^^

pandas/core/indexes/datetimes.py

+3-2
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

+11
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,14 @@ 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+
# GH16071
260+
df = pd.DataFrame(columns=['1'],
261+
index=pd.date_range('2016-10-01T00:00:00',
262+
'2016-10-01T23:59:59'))
263+
result = df.loc[datetime(2016, 10, 1):]
264+
tm.assert_frame_equal(result, df)
265+
266+
result = df.loc['2016-10-01T00:00:00':]
267+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)