Skip to content

Commit

Permalink
Fixes for #16896(TimedeltaIndex indexing regression for strings) (#16907
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jdeschenes authored and jreback committed Jul 13, 2017
1 parent 25384ba commit 4ca9fcd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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`).

I/O
^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,15 @@ def is_timedelta64_dtype(arr_or_dtype):
False
>>> is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
True
>>> is_timedelta64_dtype('0 days')
False
"""

if arr_or_dtype is None:
return False
try:
tipo = _get_dtype_type(arr_or_dtype)
except ValueError:
except:
return False
return issubclass(tipo, np.timedelta64)

Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,17 @@ def test_is_datetime64tz_dtype():

def test_is_timedelta64_dtype():
assert not com.is_timedelta64_dtype(object)
assert not com.is_timedelta64_dtype(None)
assert not com.is_timedelta64_dtype([1, 2, 3])
assert not com.is_timedelta64_dtype(np.array([], dtype=np.datetime64))
assert not com.is_timedelta64_dtype('0 days')
assert not com.is_timedelta64_dtype("0 days 00:00:00")
assert not com.is_timedelta64_dtype(["0 days 00:00:00"])
assert not com.is_timedelta64_dtype("NO DATE")

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")
assert com.is_timedelta64_dtype(pd.to_timedelta(['0 days', '1 days']))


def test_is_period_dtype():
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ 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 16896
assert idx.get_loc('0 days') == 0

def test_get_loc_nat(self):
tidx = TimedeltaIndex(['1 days 01:00:00', 'NaT', '2 days 01:00:00'])

Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/indexing/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class TestTimedeltaIndexing(object):

def test_boolean_indexing(self):
# GH 14946
df = pd.DataFrame({'x': range(10)})
Expand Down Expand Up @@ -40,3 +39,11 @@ def test_list_like_indexing(self, indexer, expected):
dtype="int64")

tm.assert_frame_equal(expected, df)

def test_string_indexing(self):
# GH 16896
df = pd.DataFrame({'x': range(3)},
index=pd.to_timedelta(range(3), unit='days'))
expected = df.iloc[0]
sliced = df.loc['0 days']
tm.assert_series_equal(sliced, expected)

0 comments on commit 4ca9fcd

Please sign in to comment.