Repository: pandas-dev/pandas

Issue:
BUG: TimedeltaIndex raising ValueError when slice indexing
This is tangentially related to #14946.

#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd

df = pd.DataFrame({'x': range(10)})
df.index = pd.to_timedelta(df.index, unit='s')

# Each of these lines work as intended
df.loc[df.index[0], 'x'] = 10
df.loc[df['x'] > 1, 'x'] = 20
df.iloc[[0, 2], 0] = 30
df.iloc[2:4, 0] = 40

# These lines fail however
df.loc[df.index[7:9], 'x'] = 50
df.loc[df.index[3:5], 'x'] = 60
```

#### Problem description

This behavior is broken since 0.19.1 and is still occurring with the latest version of pandas. The problem is pretty self-explanatory.

**ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().**

#### Expected Output

The output is self-explanatory.

It can be easily fixed by adding the following to core/indexes/timedeltas.py TimedeltaIndex:

```python
def get_loc(self, key, method=None, tolerance=None):
    if is_bool_indexer(key) or is_timedelta64_dtype(key):
        raise TypeError
```

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-53-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8

pandas: 0.18.1
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.2
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.1.4
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext lo64)
jinja2: None
boto: None
pandas_datareader: None

</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py
--- 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
--- 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):


Test patch:
diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py
--- 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
--- 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)