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: DatetimeIndex allows indexing with Timedelta and likewise for TimedeltaIndex/Timestamp #20464

Closed
jschendel opened this issue Mar 23, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@jschendel
Copy link
Member

commented Mar 23, 2018

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

@jreback

This comment has been minimized.

Copy link
Contributor

commented Mar 23, 2018

these are being treated as integers (not correct)

@jreback jreback added this to the Next Major Release milestone Mar 23, 2018

@jbrockmendel

This comment has been minimized.

Copy link
Member

commented Aug 31, 2018

Looks like this is specific to pd.Timedelta(0), trying it with pd.Timedelta(1) raises correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.