From 139a976782458a3193d3f65ec51001e57c19144f Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 18 Mar 2018 23:07:28 -0700 Subject: [PATCH 1/3] Bug: Allow np.timedelta64 objects to index TimedeltaIndex --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/core/indexes/timedeltas.py | 4 +++- pandas/tests/indexing/test_timedelta.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index cfe28edd175b6..c8a73248f98cc 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -871,6 +871,7 @@ Datetimelike - Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where name of the returned object was not always set consistently. (:issue:`19744`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where operations with numpy arrays raised ``TypeError`` (:issue:`19847`) +- Bug in :class:`TimedeltaIndex` when slicing with a ``np.timedelta64`` object would raise a ``TypeError`` (:issue:`20393`) Timedelta ^^^^^^^^^ diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index b5a08fc0168e4..d34b83deadb06 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -819,6 +819,7 @@ def _maybe_cast_slice_bound(self, label, side, kind): label : object """ + assert kind in ['ix', 'loc', 'getitem', None] if isinstance(label, compat.string_types): @@ -829,7 +830,8 @@ def _maybe_cast_slice_bound(self, label, side, kind): else: return (lbound + to_offset(parsed.resolution) - Timedelta(1, 'ns')) - elif is_integer(label) or is_float(label): + elif ((is_integer(label) or is_float(label)) and + not is_timedelta64_dtype(label)): self._invalid_indexer('slice', label) return label diff --git a/pandas/tests/indexing/test_timedelta.py b/pandas/tests/indexing/test_timedelta.py index 3ad3b771b2ab2..48ea49119356d 100644 --- a/pandas/tests/indexing/test_timedelta.py +++ b/pandas/tests/indexing/test_timedelta.py @@ -68,3 +68,15 @@ def test_listlike_setitem(self, value): series.iloc[0] = value expected = pd.Series([pd.NaT, 1, 2], dtype='timedelta64[ns]') tm.assert_series_equal(series, expected) + + @pytest.mark.parametrize('start,stop, expected_slice', [ + [np.timedelta64(0, 'ns'), None, slice(0, 11)], + [np.timedelta64(1, 'D'), np.timedelta64(6, 'D'), slice(1, 7)], + [None, np.timedelta64(4, 'D'), slice(0, 5)]]) + def test_numpy_timedelta_scalar_indexing(self, start, stop, + expected_slice): + # GH 20393 + s = pd.Series(range(11), pd.timedelta_range('0 days', '10 days')) + result = s.loc[slice(start, stop)] + expected = s.iloc[expected_slice] + tm.assert_series_equal(result, expected) From 15a393f505a7dfb1e7978bf412aa4703f7432364 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 18 Mar 2018 23:10:50 -0700 Subject: [PATCH 2/3] rephrase whatsnew and remove newline --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/core/indexes/timedeltas.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index c8a73248f98cc..99692fddef1b7 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -871,7 +871,7 @@ Datetimelike - Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where name of the returned object was not always set consistently. (:issue:`19744`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where operations with numpy arrays raised ``TypeError`` (:issue:`19847`) -- Bug in :class:`TimedeltaIndex` when slicing with a ``np.timedelta64`` object would raise a ``TypeError`` (:issue:`20393`) +- Bug in :class:`TimedeltaIndex` when indexing with a ``np.timedelta64`` object would raise a ``TypeError`` (:issue:`20393`) Timedelta ^^^^^^^^^ diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index d34b83deadb06..9757d775201cc 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -819,7 +819,6 @@ def _maybe_cast_slice_bound(self, label, side, kind): label : object """ - assert kind in ['ix', 'loc', 'getitem', None] if isinstance(label, compat.string_types): From 199e819dc426f5b31905f98d8da5d6b5d6051ca7 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 19 Mar 2018 06:05:52 -0400 Subject: [PATCH 3/3] docs --- doc/source/whatsnew/v0.23.0.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 99692fddef1b7..eda3fba043be9 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -871,7 +871,6 @@ Datetimelike - Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where name of the returned object was not always set consistently. (:issue:`19744`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where operations with numpy arrays raised ``TypeError`` (:issue:`19847`) -- Bug in :class:`TimedeltaIndex` when indexing with a ``np.timedelta64`` object would raise a ``TypeError`` (:issue:`20393`) Timedelta ^^^^^^^^^ @@ -888,7 +887,8 @@ Timedelta - Bug in :func:`Timedelta.total_seconds()` causing precision errors i.e. ``Timedelta('30S').total_seconds()==30.000000000000004`` (:issue:`19458`) - Bug in :func: `Timedelta.__rmod__` where operating with a ``numpy.timedelta64`` returned a ``timedelta64`` object instead of a ``Timedelta`` (:issue:`19820`) - Multiplication of :class:`TimedeltaIndex` by ``TimedeltaIndex`` will now raise ``TypeError`` instead of raising ``ValueError`` in cases of length mis-match (:issue`19333`) -- +- Bug in indexing a :class:`TimedeltaIndex` with a ``np.timedelta64`` object which was raising a ``TypeError`` (:issue:`20393`) + Timezones ^^^^^^^^^