Fixes for #16896(TimedeltaIndex indexing regression for strings)
State: closed

Fixed a regression for indexing with a string with a ``TimedeltaIndex`` (#16896)

I am not a fan of the catch all except, but there is no guarantee in `_get_dtype_type`. As part of this PR, we can ensure that all type checks return True/False. `_get_dtype_type` should probably return a type or a TypeError... Nothing else. Let me know if you want to do that as well.

 - [x] closes #16896
 - [x] tests added / passed
 - [x] passes ``git diff upstream/master --name-only -- '*.py' | flake8 --diff`` (On Windows, ``git diff upstream/master -u -- "*.py" | flake8 --diff`` might work as an alternative.)
 - [x] whatsnew entry


Labels:
name: Bug
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: Timedelta
description: Timedelta data type

Comments:
> I am not a fan of the catch all except, but there is no guarantee in _get_dtype_type. As part of this PR, we can ensure that all type checks return True/False. _get_dtype_type should probably return a type or a TypeError... Nothing else. Let me know if you want to do that as well.

This is pretty tricky as we cannot directly compare certain things vs a np.dtype (IOW some *may* raise). So easiest to catch on the is_* methods. Sure it could be changed to be more idiomatic (but separate PR).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=h1) Report
> Merging [#16907](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/9d13227345882daaa90f03078c09a9b44a18ce72?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16907/graphs/tree.svg?height=150&width=650&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16907      +/-   ##
==========================================
- Coverage   90.99%   90.97%   -0.02%     
==========================================
  Files         161      161              
  Lines       49301    49303       +2     
==========================================
- Hits        44861    44854       -7     
- Misses       4440     4449       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.74% <100%> (ø)` | :arrow_up: |
| #single | `40.19% <0%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/dtypes/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2NvbW1vbi5weQ==) | `94.81% <100%> (+0.02%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.71% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=footer). Last update [9d13227...3d36695](https://codecov.io/gh/pandas-dev/pandas/pull/16907?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

lgtm. ping on green.
Can you please add an explicit regression test for `TimedeltaIndex.get_loc()` as well? We use `get_loc` directly in xarray instead of going through `DataFrame.loc`.
@shoyer do you have a specific example that is failing?
I would suggest the following, which closely mimics what we do in xarray:
```python
index = pd.to_timedelta(range(3), unit='days')
assert index.get_loc('0 days') == 0
```
@jreback Let me know if you want anything else!
thanks!