Skip to content

Commit eee2b17

Browse files
author
Jean-Mathieu Deschenes
committed
BUG: TimedeltaIndex raising ValueError when slice indexing (pandas-dev#16637)
closes pandas-dev#16637
1 parent 5aba665 commit eee2b17

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Bug Fixes
5151
- Bug in :func:`unique` on an array of tuples (:issue:`16519`)
5252
- Bug in :func:`cut` when ``labels`` are set, resulting in incorrect label ordering (:issue:`16459`)
5353
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on ``Categoricals`` (:issue:`16409`)
54+
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when slice indexing with ``loc`` (:issue:`16637`)
5455

5556
Conversion
5657
^^^^^^^^^^

pandas/core/indexes/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,7 @@ def get_loc(self, key, method=None, tolerance=None):
680680
-------
681681
loc : int
682682
"""
683-
684-
if is_bool_indexer(key):
683+
if is_bool_indexer(key) or is_timedelta64_dtype(key):
685684
raise TypeError
686685

687686
if isnull(key):

pandas/tests/indexing/test_timedelta.py

+17
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ def test_boolean_indexing(self):
1818
index=pd.to_timedelta(range(10), unit='s'),
1919
columns=['x'])
2020
tm.assert_frame_equal(expected, result)
21+
22+
def test_slice_indexing(self):
23+
# GH 16637
24+
df = pd.DataFrame({'x': range(10)})
25+
df.index = pd.to_timedelta(range(10), unit='s')
26+
27+
conditions = [df.index[0], df.index[4:8], df.index[[3, 5]]]
28+
expected_data = [[20, 1, 2, 3, 4, 5, 6, 7, 8, 9],
29+
[0, 1, 2, 3, 20, 20, 20, 20, 8, 9],
30+
[0, 1, 2, 20, 4, 20, 6, 7, 8, 9]]
31+
for cond, data in zip(conditions, expected_data):
32+
result = df.copy()
33+
result.loc[cond, 'x'] = 20
34+
expected = pd.DataFrame(data,
35+
index=pd.to_timedelta(range(10), unit='s'),
36+
columns=['x'])
37+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)