From 714c2646cd7122d49242beb5ad18e29bb1200dea Mon Sep 17 00:00:00 2001 From: jdeschenes Date: Thu, 6 Jul 2017 08:23:38 -0400 Subject: [PATCH] BUG: TimedeltaIndex raising ValueError when slice indexing (#16637) (#16638) (cherry picked from commit cc5d20f50c059f02472b029f4455b2609620374e) --- doc/source/whatsnew/v0.20.3.txt | 2 ++ pandas/core/dtypes/common.py | 5 ++++- pandas/core/indexes/timedeltas.py | 3 +-- pandas/tests/dtypes/test_common.py | 4 +++- pandas/tests/indexing/test_timedelta.py | 24 +++++++++++++++++++++++- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt index 3d1bed2c9f1a9..ce7e8be16d8e2 100644 --- a/doc/source/whatsnew/v0.20.3.txt +++ b/doc/source/whatsnew/v0.20.3.txt @@ -41,6 +41,8 @@ Bug Fixes - Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`) - Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`) - Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`) +- Bug in a DataFrame/Series with a ``TimedeltaIndex`` when slice indexing (:issue:`16637`) + Conversion ^^^^^^^^^^ diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index bfec1ec3ebe8c..2eebf3704253e 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -396,7 +396,10 @@ def is_timedelta64_dtype(arr_or_dtype): if arr_or_dtype is None: return False - tipo = _get_dtype_type(arr_or_dtype) + try: + tipo = _get_dtype_type(arr_or_dtype) + except ValueError: + return False return issubclass(tipo, np.timedelta64) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index ab94a5bffb4f9..c025c74625972 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -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): diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 4633dde5ed537..ba510e68f9a21 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -200,10 +200,12 @@ def test_is_datetime64tz_dtype(): def test_is_timedelta64_dtype(): assert not com.is_timedelta64_dtype(object) assert not com.is_timedelta64_dtype([1, 2, 3]) - + assert not com.is_timedelta64_dtype(np.array([], dtype=np.datetime64)) assert com.is_timedelta64_dtype(np.timedelta64) assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]")) + assert not com.is_timedelta64_dtype("0 days 00:00:00") + def test_is_period_dtype(): assert not com.is_period_dtype(object) diff --git a/pandas/tests/indexing/test_timedelta.py b/pandas/tests/indexing/test_timedelta.py index cf8cc6c2d345d..be3ea8f0c371d 100644 --- a/pandas/tests/indexing/test_timedelta.py +++ b/pandas/tests/indexing/test_timedelta.py @@ -1,3 +1,5 @@ +import pytest + import pandas as pd from pandas.util import testing as tm @@ -16,5 +18,25 @@ def test_boolean_indexing(self): result = df.assign(x=df.mask(cond, 10).astype('int64')) expected = pd.DataFrame(data, index=pd.to_timedelta(range(10), unit='s'), - columns=['x']) + columns=['x'], + dtype='int64') tm.assert_frame_equal(expected, result) + + @pytest.mark.parametrize( + "indexer, expected", + [(0, [20, 1, 2, 3, 4, 5, 6, 7, 8, 9]), + (slice(4, 8), [0, 1, 2, 3, 20, 20, 20, 20, 8, 9]), + ([3, 5], [0, 1, 2, 20, 4, 20, 6, 7, 8, 9])]) + def test_list_like_indexing(self, indexer, expected): + # GH 16637 + df = pd.DataFrame({'x': range(10)}, dtype="int64") + df.index = pd.to_timedelta(range(10), unit='s') + + df.loc[df.index[indexer], 'x'] = 20 + + expected = pd.DataFrame(expected, + index=pd.to_timedelta(range(10), unit='s'), + columns=['x'], + dtype="int64") + + tm.assert_frame_equal(expected, df)