Skip to content

Commit

Permalink
BUG: fix DatetimeIndex._maybe_cast_slice_bound for empty index (GH143…
Browse files Browse the repository at this point in the history
…54) (#14501)
  • Loading branch information
jorisvandenbossche authored Oct 27, 2016
1 parent e7ac84d commit d7fb5bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down Expand Up @@ -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`)
5 changes: 3 additions & 2 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit d7fb5bd

Please sign in to comment.