Skip to content

Indexing TimedeltaIndex with np.timedelta64 #16909

Closed
@shoyer

Description

@shoyer

On pandas 0.20.2, TimedeltaIndex.get_loc() worked on np.timedelta64, even though DataFrame.loc fails:

In [2]: df = pd.DataFrame({'x': range(3)}, pd.to_timedelta(range(3), unit='days'))

In [3]: df.loc[df.index[0].to_timedelta64()]
TypeError: cannot do index indexing on <class 'pandas.core.indexes.timedeltas.TimedeltaIndex'> with these indexers [0] of <class 'numpy.timedelta64'>

In [5]: df.index.get_loc(df.index[0].to_timedelta64())
Out[5]: 0

Now, both raise TypeError. (This is similar to the issue in #16896)

I think some xarray users are were likely relying on this (we use get_loc() directly), but I think supporting this could also be useful for pandas users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions