Skip to content

BUG: DatetimeIndex allows indexing with Timedelta and likewise for TimedeltaIndex/Timestamp #20464

Closed
@jschendel

Description

@jschendel

Code Sample, a copy-pastable example if possible

A Series or DataFrame with DatetimeIndex can currently be indexed with a Timedelta:

In [2]: s = pd.Series(list('abc'), pd.date_range(0, periods=3))

In [3]: s
Out[3]:
1970-01-01    a
1970-01-02    b
1970-01-03    c
Freq: D, dtype: object

In [4]: s.loc[pd.Timedelta(0)]
Out[4]: 'a'

In [5]: s.loc[pd.Timedelta('0 days'):pd.Timedelta('1 day')]
Out[5]:
1970-01-01    a
1970-01-02    b
Freq: D, dtype: object

Likewise for TimedeltaIndex and Timestamp:

In [6]: s = pd.Series(list('abc'), pd.timedelta_range(0, periods=3))

In [7]: s
Out[7]:
0 days    a
1 days    b
2 days    c
Freq: D, dtype: object

In [8]: s.loc[pd.Timestamp(0)]
Out[8]: 'a'

In [9]: s.loc[pd.Timestamp('1970-01-01'):pd.Timestamp('1970-01-02')]
Out[9]:
0 days    a
1 days    b
Freq: D, dtype: object

Problem description

Matches are being returned for labels that are not in the index.

Similar to #20432, but doesn't seem as widespread.

Expected Output

I'd expect a KeyError to be raised.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 85817a7
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+667.g85817a7
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.0
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexingRelated 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