Skip to content

Commit 0b05168

Browse files
author
Jean-Mathieu Deschenes
committed
Fix for #16909
get_loc should now work for np.timedelta64 data type.
1 parent 4ca9fcd commit 0b05168

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Indexing
155155
- 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`).
156156
- 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`).
157157
- Fixes regression in 0.20.3 when indexing with a string on a ``TimedeltaIndex`` (:issue:`16896`).
158+
- Fixed ``TimedeltaIndex.get_loc`` ``for np.timedelta64`` data type(:issue:`16909`).
158159

159160
I/O
160161
^^^

pandas/core/indexes/timedeltas.py

+1-1
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

+2
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
# GH 16896
7072
assert idx.get_loc('0 days') == 0
7173

0 commit comments

Comments
 (0)