Skip to content

Commit

Permalink
BUG: TimedeltaIndex raising ValueError when slice indexing (pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeschenes authored and jreback committed Jul 6, 2017
1 parent 15db50b commit cc5d20f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Bug Fixes
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
- 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`)
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
- Bug in a DataFrame/Series with a ``TimedeltaIndex`` when slice indexing (:issue:`16637`)


Conversion
^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion pandas/tests/indexing/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import pandas as pd
from pandas.util import testing as tm

Expand All @@ -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)

0 comments on commit cc5d20f

Please sign in to comment.