Skip to content

Commit 5ab186c

Browse files
author
Jean-Mathieu Deschenes
committed
Fix for #16909
get_loc should now work for np.timedelta64 data type.
1 parent 9d13227 commit 5ab186c

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Indexing
155155

156156
- When called with a null slice (e.g. ``df.iloc[:]``), the ``.iloc`` and ``.loc`` indexers return a shallow copy of the original object. Previously they returned the original object. (:issue:`13873`).
157157
- When called on an unsorted ``MultiIndex``, the ``loc`` indexer now will raise ``UnsortedIndexError`` only if proper slicing is used on non-sorted levels (:issue:`16734`).
158-
158+
- Fixed ``TimedeltaIndex.get_loc`` ``for np.timedelta64`` data type(:issue:`16909`).
159159

160160
I/O
161161
^^^

pandas/core/indexes/timedeltas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def get_loc(self, key, method=None, tolerance=None):
682682
-------
683683
loc : int
684684
"""
685-
if is_bool_indexer(key) or is_timedelta64_dtype(key):
685+
if is_list_like(key):
686686
raise TypeError
687687

688688
if isnull(key):

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def test_get_loc(self):
6666
for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
6767
assert idx.get_loc('1 day 1 hour', method) == loc
6868

69+
assert idx.get_loc(idx[1].to_timedelta64()) == 1
70+
6971
def test_get_loc_nat(self):
7072
tidx = TimedeltaIndex(['1 days 01:00:00', 'NaT', '2 days 01:00:00'])
7173

0 commit comments

Comments
 (0)