From 0b051682abea2a49c1042c9aac74c701fd3b03e8 Mon Sep 17 00:00:00 2001 From: Jean-Mathieu Deschenes Date: Thu, 13 Jul 2017 16:13:59 -0400 Subject: [PATCH 1/3] Fix for #16909 get_loc should now work for np.timedelta64 data type. --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/indexes/timedeltas.py | 2 +- pandas/tests/indexes/timedeltas/test_timedelta.py | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 039b24cc63217..c1f99e55f934a 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -155,6 +155,7 @@ Indexing - 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`). - 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`). - Fixes regression in 0.20.3 when indexing with a string on a ``TimedeltaIndex`` (:issue:`16896`). +- Fixed ``TimedeltaIndex.get_loc`` ``for np.timedelta64`` data type(:issue:`16909`). I/O ^^^ diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index faec813df3993..c8573989090e9 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -682,7 +682,7 @@ def get_loc(self, key, method=None, tolerance=None): ------- loc : int """ - if is_bool_indexer(key) or is_timedelta64_dtype(key): + if is_list_like(key): raise TypeError if isnull(key): diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index a4fc26382fb9b..0b52b4f31844a 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -66,6 +66,8 @@ def test_get_loc(self): for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]: assert idx.get_loc('1 day 1 hour', method) == loc + assert idx.get_loc(idx[1].to_timedelta64()) == 1 + # GH 16896 assert idx.get_loc('0 days') == 0 From 13de27d13378096955af73afd854d08df25a2226 Mon Sep 17 00:00:00 2001 From: Jean-Mathieu Deschenes Date: Thu, 13 Jul 2017 17:26:40 -0400 Subject: [PATCH 2/3] Fixes from comments of @gfyoung * Fix Lint Error --- pandas/core/indexes/timedeltas.py | 2 +- pandas/tests/indexes/timedeltas/test_timedelta.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index c8573989090e9..68713743d72ed 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -15,7 +15,7 @@ _ensure_int64) from pandas.core.dtypes.missing import isnull from pandas.core.dtypes.generic import ABCSeries -from pandas.core.common import _maybe_box, _values_from_object, is_bool_indexer +from pandas.core.common import _maybe_box, _values_from_object from pandas.core.indexes.base import Index from pandas.core.indexes.numeric import Int64Index diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 0b52b4f31844a..59e4b1432b8bc 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -66,6 +66,7 @@ def test_get_loc(self): for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]: assert idx.get_loc('1 day 1 hour', method) == loc + # GH 16909 assert idx.get_loc(idx[1].to_timedelta64()) == 1 # GH 16896 From a7e2d74a7608ead5e70860ab64d4c399877d96ea Mon Sep 17 00:00:00 2001 From: Jean-Mathieu Deschenes Date: Thu, 13 Jul 2017 18:14:02 -0400 Subject: [PATCH 3/3] Made a mistake in what's new --- doc/source/whatsnew/v0.21.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index c1f99e55f934a..2716d9b09eaa9 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -155,7 +155,7 @@ Indexing - 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`). - 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`). - Fixes regression in 0.20.3 when indexing with a string on a ``TimedeltaIndex`` (:issue:`16896`). -- Fixed ``TimedeltaIndex.get_loc`` ``for np.timedelta64`` data type(:issue:`16909`). +- Fixed ``TimedeltaIndex.get_loc`` handling of ``np.timedelta64`` inputs (:issue:`16909`). I/O ^^^