Skip to content

Commit ffcb993

Browse files
BUG: fix DatetimeIndex._maybe_cast_slice_bound for empty index (GH14354)
1 parent e3d943d commit ffcb993

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/whatsnew/v0.19.1.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Bug Fixes
3535

3636

3737

38-
3938
- Bug in localizing an ambiguous timezone when a boolean is passed (:issue:`14402`)
4039
- Bug in ``TimedeltaIndex`` addition with a Datetime-like object where addition overflow in the negative direction was not being caught (:issue:`14068`, :issue:`14453`)
4140

@@ -77,4 +76,7 @@ Bug Fixes
7776

7877

7978

80-
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` is not scalar and ``values`` is not specified (:issue:`14380`)
79+
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
80+
is not scalar and ``values`` is not specified (:issue:`14380`)
81+
82+
- Regression in ``DatetimeIndex._maybe_cast_slice_bound`` when index is empty (:issue:`14354`).

pandas/tseries/index.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1453,8 +1453,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
14531453
# lower, upper form the half-open interval:
14541454
# [parsed, parsed + 1 freq)
14551455
# because label may be passed to searchsorted
1456-
# the bounds need swapped if index is reverse sorted
1457-
if self.is_monotonic_decreasing:
1456+
# the bounds need swapped if index is reverse sorted and has a
1457+
# length (is_monotonic_decreasing gives True for empty index)
1458+
if self.is_monotonic_decreasing and len(self):
14581459
return upper if side == 'left' else lower
14591460
return lower if side == 'left' else upper
14601461
else:

pandas/tseries/tests/test_timeseries.py

+13
Original file line numberDiff line numberDiff line change
@@ -3911,6 +3911,19 @@ def test_slice_with_zero_step_raises(self):
39113911
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
39123912
lambda: ts.ix[::0])
39133913

3914+
def test_slice_bounds_empty(self):
3915+
# GH 14354
3916+
empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015')
3917+
3918+
right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc')
3919+
exp = Timestamp('2015-01-02 23:59:59.999999999')
3920+
self.assertEqual(right, exp)
3921+
3922+
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
3923+
exp = Timestamp('2015-01-02 00:00:00')
3924+
self.assertEqual(left, exp)
3925+
3926+
39143927

39153928
class TestDatetime64(tm.TestCase):
39163929
"""

0 commit comments

Comments
 (0)