Skip to content

Commit 4ca9fcd

Browse files
jdeschenesjreback
authored andcommitted
Fixes for #16896(TimedeltaIndex indexing regression for strings) (#16907)
1 parent 25384ba commit 4ca9fcd

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Indexing
154154

155155
- 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`).
156156
- 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`).
157-
157+
- Fixes regression in 0.20.3 when indexing with a string on a ``TimedeltaIndex`` (:issue:`16896`).
158158

159159
I/O
160160
^^^

pandas/core/dtypes/common.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,15 @@ def is_timedelta64_dtype(arr_or_dtype):
392392
False
393393
>>> is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
394394
True
395+
>>> is_timedelta64_dtype('0 days')
396+
False
395397
"""
396398

397399
if arr_or_dtype is None:
398400
return False
399401
try:
400402
tipo = _get_dtype_type(arr_or_dtype)
401-
except ValueError:
403+
except:
402404
return False
403405
return issubclass(tipo, np.timedelta64)
404406

pandas/tests/dtypes/test_common.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,17 @@ def test_is_datetime64tz_dtype():
199199

200200
def test_is_timedelta64_dtype():
201201
assert not com.is_timedelta64_dtype(object)
202+
assert not com.is_timedelta64_dtype(None)
202203
assert not com.is_timedelta64_dtype([1, 2, 3])
203204
assert not com.is_timedelta64_dtype(np.array([], dtype=np.datetime64))
205+
assert not com.is_timedelta64_dtype('0 days')
206+
assert not com.is_timedelta64_dtype("0 days 00:00:00")
207+
assert not com.is_timedelta64_dtype(["0 days 00:00:00"])
208+
assert not com.is_timedelta64_dtype("NO DATE")
209+
204210
assert com.is_timedelta64_dtype(np.timedelta64)
205211
assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
206-
207-
assert not com.is_timedelta64_dtype("0 days 00:00:00")
212+
assert com.is_timedelta64_dtype(pd.to_timedelta(['0 days', '1 days']))
208213

209214

210215
def test_is_period_dtype():

pandas/tests/indexes/timedeltas/test_timedelta.py

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def test_get_loc(self):
6666
for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
6767
assert idx.get_loc('1 day 1 hour', method) == loc
6868

69+
# GH 16896
70+
assert idx.get_loc('0 days') == 0
71+
6972
def test_get_loc_nat(self):
7073
tidx = TimedeltaIndex(['1 days 01:00:00', 'NaT', '2 days 01:00:00'])
7174

pandas/tests/indexing/test_timedelta.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
class TestTimedeltaIndexing(object):
8-
98
def test_boolean_indexing(self):
109
# GH 14946
1110
df = pd.DataFrame({'x': range(10)})
@@ -40,3 +39,11 @@ def test_list_like_indexing(self, indexer, expected):
4039
dtype="int64")
4140

4241
tm.assert_frame_equal(expected, df)
42+
43+
def test_string_indexing(self):
44+
# GH 16896
45+
df = pd.DataFrame({'x': range(3)},
46+
index=pd.to_timedelta(range(3), unit='days'))
47+
expected = df.iloc[0]
48+
sliced = df.loc['0 days']
49+
tm.assert_series_equal(sliced, expected)

0 commit comments

Comments
 (0)