Skip to content

BUG: fix DatetimeIndex._maybe_cast_slice_bound for empty index (GH14354) #14501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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