Skip to content

Commit

Permalink
BUG: TimedeltaIndex raising ValueError when slice indexing (pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Mathieu Deschenes committed Jun 8, 2017
1 parent 10c17d4 commit 8e63973
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Bug Fixes
- Bug in :func:`unique` on an array of tuples (:issue:`16519`)
- Bug in :func:`cut` when ``labels`` are set, resulting in incorrect label ordering (:issue:`16459`)
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on ``Categoricals`` (:issue:`16409`)
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when slice indexing with ``loc`` (:issue:`16637`)

Conversion
^^^^^^^^^^
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,7 @@ def get_loc(self, key, method=None, tolerance=None):
-------
loc : int
"""

if is_bool_indexer(key):
if is_bool_indexer(key) or is_timedelta64_dtype(key):
raise TypeError

if isnull(key):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexing/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ def test_boolean_indexing(self):
index=pd.to_timedelta(range(10), unit='s'),
columns=['x'])
tm.assert_frame_equal(expected, result)

def test_slice_indexing(self):
# GH 16637
df = pd.DataFrame({'x': range(10)})
df.index = pd.to_timedelta(range(10), unit='s')

conditions = [df.index[0], df.index[4:8], df.index[[3, 5]]]
expected_data = [[20, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 20, 20, 20, 20, 8, 9],
[0, 1, 2, 20, 4, 20, 6, 7, 8, 9]]
for cond, data in zip(conditions, expected_data):
result = df.copy()
result.loc[cond, 'x'] = 20
expected = pd.DataFrame(data,
index=pd.to_timedelta(range(10), unit='s'),
columns=['x'])
tm.assert_frame_equal(expected, result)

0 comments on commit 8e63973

Please sign in to comment.