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

Fixes for #16896(TimedeltaIndex indexing regression for strings) #16907

Merged
merged 7 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -155,7 +155,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 that raises an index error when a string is used for ``TimedeltaIndex`` (:issue:`16896`).
Copy link
Contributor

Choose a reason for hiding this comment

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

can you be a bit more explicty (e.g. say indexing with a string when you have a TimedeltaIndex)


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
5 changes: 4 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,15 @@ 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 com.is_timedelta64_dtype(np.timedelta64)
assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
Copy link
Contributor

Choose a reason for hiding this comment

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

also can you add: com.is_timedelta64_dtype(pd.to_timedelta(['0 days', '1 days'])) IOW a TDI


assert not com.is_timedelta64_dtype('0 days')
Copy link
Contributor

Choose a reason for hiding this comment

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

silly but can you group all of the assert not ones together (IIRC pattern is to have them first)

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")


def test_is_period_dtype():
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexing/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ def test_list_like_indexing(self, indexer, expected):
dtype="int64")

tm.assert_frame_equal(expected, df)

def test_string_indexing(self):
df = pd.DataFrame({'x': range(3)}, index=pd.to_timedelta(range(3), unit='days'))
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the issue number here

expected = df.iloc[0]
sliced = df.loc['0 days']
tm.assert_series_equal(sliced, expected)