Skip to content

BUG: DatetimeIndex with time object as key #8907

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 1 commit into from
Nov 29, 2014
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Bug Fixes
- ``sql_schema`` now generates dialect appropriate ``CREATE TABLE`` statements (:issue:`8697`)
- ``slice`` string method now takes step into account (:issue:`8754`)
- Bug in ``BlockManager`` where setting values with different type would break block integrity (:issue:`8850`)
- Bug in ``DatetimeIndex`` when using ``time`` object as key (:issue:`8667`)
- Fix negative step support for label-based slices (:issue:`8753`)

Old behavior:
Expand Down
10 changes: 8 additions & 2 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,14 @@ cdef class DatetimeEngine(Int64Engine):
val = _to_i8(val)
return self._get_loc_duplicates(val)
values = self._get_index_values()
conv = _to_i8(val)
loc = values.searchsorted(conv, side='left')

try:
conv = _to_i8(val)
loc = values.searchsorted(conv, side='left')
except TypeError:
self._date_check_type(val)
raise KeyError(val)

if loc == len(values) or util.get_value_at(values, loc) != conv:
raise KeyError(val)
return loc
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,27 @@ def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
self.assertEqual(str(index.reindex([])[0].tz), 'US/Eastern')
self.assertEqual(str(index.reindex(np.array([]))[0].tz), 'US/Eastern')

def test_time_loc(self): # GH8667
from datetime import time
from pandas.index import _SIZE_CUTOFF

ns = _SIZE_CUTOFF + np.array([-100, 100])
key = time(15, 11, 30)
start = key.hour * 3600 + key.minute * 60 + key.second
step = 24 * 3600

for n in ns:
idx = pd.date_range('2014-11-26', periods=n, freq='S')
ts = pd.Series(np.random.randn(n), index=idx)
i = np.arange(start, n, step)

tm.assert_array_equal(ts.index.get_loc(key), i)
tm.assert_series_equal(ts[key], ts.iloc[i])

left, right = ts.copy(), ts.copy()
left[key] *= -10
right.iloc[i] *= -10
tm.assert_series_equal(left, right)

class TestPeriodIndex(Base, tm.TestCase):
_holder = PeriodIndex
Expand Down
14 changes: 7 additions & 7 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,10 @@ def get_value(self, series, key):

return self.get_value_maybe_box(series, key)

if isinstance(key, time):
locs = self.indexer_at_time(key)
return series.take(locs)

try:
return _maybe_box(self, Index.get_value(self, series, key), series, key)
except KeyError:
Expand All @@ -1219,10 +1223,6 @@ def get_value(self, series, key):
except (TypeError, ValueError, KeyError):
pass

if isinstance(key, time):
locs = self.indexer_at_time(key)
return series.take(locs)

try:
return self.get_value_maybe_box(series, key)
except (TypeError, ValueError, KeyError):
Expand Down Expand Up @@ -1250,6 +1250,9 @@ def get_loc(self, key):
stamp = Timestamp(key, tz=self.tz)
return self._engine.get_loc(stamp)

if isinstance(key, time):
return self.indexer_at_time(key)

try:
return Index.get_loc(self, key)
except (KeyError, ValueError):
Expand All @@ -1258,9 +1261,6 @@ def get_loc(self, key):
except (TypeError, KeyError, ValueError):
pass

if isinstance(key, time):
return self.indexer_at_time(key)

try:
stamp = Timestamp(key, tz=self.tz)
return self._engine.get_loc(stamp)
Expand Down