Skip to content

Commit cc5d20f

Browse files
jdeschenesjreback
authored andcommitted
BUG: TimedeltaIndex raising ValueError when slice indexing (#16637) (#16638)
1 parent 15db50b commit cc5d20f

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

doc/source/whatsnew/v0.20.3.txt

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Bug Fixes
4141
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
4242
- 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`)
4343
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
44+
- Bug in a DataFrame/Series with a ``TimedeltaIndex`` when slice indexing (:issue:`16637`)
45+
4446

4547
Conversion
4648
^^^^^^^^^^

pandas/core/dtypes/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,10 @@ def is_timedelta64_dtype(arr_or_dtype):
396396

397397
if arr_or_dtype is None:
398398
return False
399-
tipo = _get_dtype_type(arr_or_dtype)
399+
try:
400+
tipo = _get_dtype_type(arr_or_dtype)
401+
except ValueError:
402+
return False
400403
return issubclass(tipo, np.timedelta64)
401404

402405

pandas/core/indexes/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,7 @@ def get_loc(self, key, method=None, tolerance=None):
680680
-------
681681
loc : int
682682
"""
683-
684-
if is_bool_indexer(key):
683+
if is_bool_indexer(key) or is_timedelta64_dtype(key):
685684
raise TypeError
686685

687686
if isnull(key):

pandas/tests/dtypes/test_common.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ def test_is_datetime64tz_dtype():
200200
def test_is_timedelta64_dtype():
201201
assert not com.is_timedelta64_dtype(object)
202202
assert not com.is_timedelta64_dtype([1, 2, 3])
203-
203+
assert not com.is_timedelta64_dtype(np.array([], dtype=np.datetime64))
204204
assert com.is_timedelta64_dtype(np.timedelta64)
205205
assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
206206

207+
assert not com.is_timedelta64_dtype("0 days 00:00:00")
208+
207209

208210
def test_is_period_dtype():
209211
assert not com.is_period_dtype(object)

pandas/tests/indexing/test_timedelta.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
import pandas as pd
24
from pandas.util import testing as tm
35

@@ -16,5 +18,25 @@ def test_boolean_indexing(self):
1618
result = df.assign(x=df.mask(cond, 10).astype('int64'))
1719
expected = pd.DataFrame(data,
1820
index=pd.to_timedelta(range(10), unit='s'),
19-
columns=['x'])
21+
columns=['x'],
22+
dtype='int64')
2023
tm.assert_frame_equal(expected, result)
24+
25+
@pytest.mark.parametrize(
26+
"indexer, expected",
27+
[(0, [20, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
28+
(slice(4, 8), [0, 1, 2, 3, 20, 20, 20, 20, 8, 9]),
29+
([3, 5], [0, 1, 2, 20, 4, 20, 6, 7, 8, 9])])
30+
def test_list_like_indexing(self, indexer, expected):
31+
# GH 16637
32+
df = pd.DataFrame({'x': range(10)}, dtype="int64")
33+
df.index = pd.to_timedelta(range(10), unit='s')
34+
35+
df.loc[df.index[indexer], 'x'] = 20
36+
37+
expected = pd.DataFrame(expected,
38+
index=pd.to_timedelta(range(10), unit='s'),
39+
columns=['x'],
40+
dtype="int64")
41+
42+
tm.assert_frame_equal(expected, df)

0 commit comments

Comments
 (0)