Skip to content

BUG: Datetime MultiIndex Regression #35858

Closed
@matthewgilbert

Description

@matthewgilbert
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas

date = pandas.Timestamp("2000")
x = pandas.DataFrame([
    ["a", date, 1],
], columns=["a", "b", "c"]).set_index(["a", "b"])["c"]
x.loc[:, [date]]


InvalidIndexError: [Timestamp('2000-01-01 00:00:00')]

Problem description

The problem stems from pandas.DatetimeIndex.get_loc raising a InvalidIndexError in 1.* instead of a TypeError as in 0.25.3

0.25.3

import pandas
level_index = pandas.DatetimeIndex(['2000-01-01'], dtype='datetime64[ns]', name='b', freq=None)
key = [pandas.Timestamp('2000-01-01 00:00:00')]
level_index.get_loc(key)

...
TypeError: Cannot convert input [[Timestamp('2000-01-01 00:00:00')]] of type <class 'list'> to Timestamp

1.1.0

import pandas
level_index = pandas.DatetimeIndex(['2000-01-01'], dtype='datetime64[ns]', name='b', freq=None)
key = [pandas.Timestamp('2000-01-01 00:00:00')]
level_index.get_loc(key)

...
InvalidIndexError: [Timestamp('2000-01-01 00:00:00')]

Previously the TypeError was handled by

except TypeError:

Expected Output

I would expect this to behave similar to 0.25.3 which is valid indexing synatx.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.6.11.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-42-generic
Version : #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.16.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : 0.29.21
pytest : 5.3.5
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.12.0-RAY
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.1
sqlalchemy : 1.3.19
tables : 3.5.1
tabulate : None
xarray : 0.15.0
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndexRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions