Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: TimedeltaIndex raising ValueError when slice indexing (#16637) #16638

Merged
merged 13 commits into from
Jul 6, 2017
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine
but need tests that hit this

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't you have 2 cases that trigger the exception? if so pls add the other one.

Copy link
Contributor Author

@jdeschenes jdeschenes Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I am aware of. There is a separate issue I think were isnull would trigger an exception in the get_loc function. I don't see how that condition could be hit in the future.



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)