Skip to content

Commit d7fb5bd

Browse files
BUG: fix DatetimeIndex._maybe_cast_slice_bound for empty index (GH14354) (pandas-dev#14501)
1 parent e7ac84d commit d7fb5bd

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

doc/source/whatsnew/v0.19.1.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Bug Fixes
4848

4949
- Bug in ``RangeIndex.intersection`` when result is a empty set (:issue:`14364`).
5050
- Bug in union of differences from a ``DatetimeIndex`; this is a regression in 0.19.0 from 0.18.1 (:issue:`14323`)
51+
- Regression in ``DatetimeIndex._maybe_cast_slice_bound`` when index is empty (:issue:`14354`).
5152

5253
- Bug in groupby-transform broadcasting that could cause incorrect dtype coercion (:issue:`14457`)
5354

@@ -78,4 +79,5 @@ Bug Fixes
7879

7980

8081

81-
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` is not scalar and ``values`` is not specified (:issue:`14380`)
82+
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
83+
is not scalar and ``values`` is not specified (:issue:`14380`)

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

+12
Original file line numberDiff line numberDiff line change
@@ -3911,6 +3911,18 @@ 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+
39143926

39153927
class TestDatetime64(tm.TestCase):
39163928
"""

0 commit comments

Comments
 (0)