Skip to content

Commit e2f8f0a

Browse files
committed
Merge pull request #8907 from behzadnouri/time-slice
BUG: DatetimeIndex with time object as key
2 parents e9ee47c + 0ef5c07 commit e2f8f0a

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Bug Fixes
9797
- ``sql_schema`` now generates dialect appropriate ``CREATE TABLE`` statements (:issue:`8697`)
9898
- ``slice`` string method now takes step into account (:issue:`8754`)
9999
- Bug in ``BlockManager`` where setting values with different type would break block integrity (:issue:`8850`)
100+
- Bug in ``DatetimeIndex`` when using ``time`` object as key (:issue:`8667`)
100101
- Fix negative step support for label-based slices (:issue:`8753`)
101102

102103
Old behavior:

pandas/index.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,14 @@ cdef class DatetimeEngine(Int64Engine):
545545
val = _to_i8(val)
546546
return self._get_loc_duplicates(val)
547547
values = self._get_index_values()
548-
conv = _to_i8(val)
549-
loc = values.searchsorted(conv, side='left')
548+
549+
try:
550+
conv = _to_i8(val)
551+
loc = values.searchsorted(conv, side='left')
552+
except TypeError:
553+
self._date_check_type(val)
554+
raise KeyError(val)
555+
550556
if loc == len(values) or util.get_value_at(values, loc) != conv:
551557
raise KeyError(val)
552558
return loc

pandas/tests/test_index.py

+21
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,27 @@ def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
18861886
self.assertEqual(str(index.reindex([])[0].tz), 'US/Eastern')
18871887
self.assertEqual(str(index.reindex(np.array([]))[0].tz), 'US/Eastern')
18881888

1889+
def test_time_loc(self): # GH8667
1890+
from datetime import time
1891+
from pandas.index import _SIZE_CUTOFF
1892+
1893+
ns = _SIZE_CUTOFF + np.array([-100, 100])
1894+
key = time(15, 11, 30)
1895+
start = key.hour * 3600 + key.minute * 60 + key.second
1896+
step = 24 * 3600
1897+
1898+
for n in ns:
1899+
idx = pd.date_range('2014-11-26', periods=n, freq='S')
1900+
ts = pd.Series(np.random.randn(n), index=idx)
1901+
i = np.arange(start, n, step)
1902+
1903+
tm.assert_array_equal(ts.index.get_loc(key), i)
1904+
tm.assert_series_equal(ts[key], ts.iloc[i])
1905+
1906+
left, right = ts.copy(), ts.copy()
1907+
left[key] *= -10
1908+
right.iloc[i] *= -10
1909+
tm.assert_series_equal(left, right)
18891910

18901911
class TestPeriodIndex(Base, tm.TestCase):
18911912
_holder = PeriodIndex

pandas/tseries/index.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,10 @@ def get_value(self, series, key):
12101210

12111211
return self.get_value_maybe_box(series, key)
12121212

1213+
if isinstance(key, time):
1214+
locs = self.indexer_at_time(key)
1215+
return series.take(locs)
1216+
12131217
try:
12141218
return _maybe_box(self, Index.get_value(self, series, key), series, key)
12151219
except KeyError:
@@ -1219,10 +1223,6 @@ def get_value(self, series, key):
12191223
except (TypeError, ValueError, KeyError):
12201224
pass
12211225

1222-
if isinstance(key, time):
1223-
locs = self.indexer_at_time(key)
1224-
return series.take(locs)
1225-
12261226
try:
12271227
return self.get_value_maybe_box(series, key)
12281228
except (TypeError, ValueError, KeyError):
@@ -1250,6 +1250,9 @@ def get_loc(self, key):
12501250
stamp = Timestamp(key, tz=self.tz)
12511251
return self._engine.get_loc(stamp)
12521252

1253+
if isinstance(key, time):
1254+
return self.indexer_at_time(key)
1255+
12531256
try:
12541257
return Index.get_loc(self, key)
12551258
except (KeyError, ValueError):
@@ -1258,9 +1261,6 @@ def get_loc(self, key):
12581261
except (TypeError, KeyError, ValueError):
12591262
pass
12601263

1261-
if isinstance(key, time):
1262-
return self.indexer_at_time(key)
1263-
12641264
try:
12651265
stamp = Timestamp(key, tz=self.tz)
12661266
return self._engine.get_loc(stamp)

0 commit comments

Comments
 (0)