diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 7594478ada41a..a81ab6ed0311c 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -48,6 +48,7 @@ Bug Fixes - Bug in ``RangeIndex.intersection`` when result is a empty set (:issue:`14364`). - Bug in union of differences from a ``DatetimeIndex`; this is a regression in 0.19.0 from 0.18.1 (:issue:`14323`) +- Regression in ``DatetimeIndex._maybe_cast_slice_bound`` when index is empty (:issue:`14354`). - Bug in groupby-transform broadcasting that could cause incorrect dtype coercion (:issue:`14457`) @@ -78,4 +79,5 @@ Bug Fixes -- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` is not scalar and ``values`` is not specified (:issue:`14380`) +- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` + is not scalar and ``values`` is not specified (:issue:`14380`) \ No newline at end of file diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index f68750e242f1f..70e2d2c121773 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1453,8 +1453,9 @@ def _maybe_cast_slice_bound(self, label, side, kind): # lower, upper form the half-open interval: # [parsed, parsed + 1 freq) # because label may be passed to searchsorted - # the bounds need swapped if index is reverse sorted - if self.is_monotonic_decreasing: + # 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): return upper if side == 'left' else lower return lower if side == 'left' else upper else: diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index c13805d383e5d..aa8a5d10cd9d3 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -3911,6 +3911,18 @@ def test_slice_with_zero_step_raises(self): self.assertRaisesRegexp(ValueError, 'slice step cannot be zero', lambda: ts.ix[::0]) + def test_slice_bounds_empty(self): + # GH 14354 + empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015') + + right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc') + exp = Timestamp('2015-01-02 23:59:59.999999999') + self.assertEqual(right, exp) + + left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc') + exp = Timestamp('2015-01-02 00:00:00') + self.assertEqual(left, exp) + class TestDatetime64(tm.TestCase): """