Skip to content

BUG: MultiIndex columns containing datetime cannot be used (regression from 1.0.5 to master) #35015

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

Closed
2 of 3 tasks
Dr-Irv opened this issue Jun 26, 2020 · 4 comments · Fixed by #35140
Closed
2 of 3 tasks
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Jun 26, 2020

  • 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

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '1.1.0.dev0+1967.gf5b2e5a9a'

In [3]: expected_column_index = pd.MultiIndex.from_tuples(
   ...:     [(pd.to_datetime("02/29/2020"), pd.to_datetime("03/01/2020"))],
   ...:     names=[
   ...:         'a', 'b'])

In [4]: pd.DataFrame([], columns=expected_column_index)
---------------------------------------------------------------------------
InvalidIndexError

Problem description

This works in 1.0.5, but not in master.

See discussion in #34748 . This new issue should be resolved before that PR moves forward.

Expected Output

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '1.0.5'

In [3]:  expected_column_index = pd.MultiIndex.from_tuples(
   ...:        [(pd.to_datetime("02/29/2020"), pd.to_datetime("03/01/2020"))],
   ...:        names=[
   ...:            'a', 'b'])

In [4]: pd.DataFrame([], columns=expected_column_index)
Out[4]:
Empty DataFrame
Columns: [(2020-02-29 00:00:00, 2020-03-01 00:00:00)]
Index: []

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : f5b2e5a python : 3.7.6.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.18362 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : None.None

pandas : 1.1.0.dev0+1967.gf5b2e5a9a
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 42.0.2.post20191201
Cython : 0.29.20
pytest : 5.4.3
hypothesis : 5.18.0
sphinx : 3.1.1
blosc : None
feather : None
xlsxwriter : 1.2.9
lxml.etree : 4.5.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.15.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fsspec : 0.7.4
fastparquet : 0.4.0
gcsfs : None
matplotlib : 3.1.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
pyxlsb : 1.0.6
s3fs : 0.4.2
scipy : 1.3.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.46.0

@Dr-Irv Dr-Irv added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 26, 2020
@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jun 29, 2020

@jbrockmendel This error seems to be related to code you were working on with respect to the datetime index. The error that is raised is due to a commit you did here: 0b0cd08

Can you see if you can fix this and then I can update PR #34748 ?

@jbrockmendel
Copy link
Member

@Dr-Irv looking at this, it appears that a sufficient fix is to add in core.indexing._LocIndexer._convert_to_indexer:

            try:
                return labels.get_loc(key)
            except LookupError:
                if isinstance(key, tuple) and isinstance(labels, ABCMultiIndex):
                    if len(key) == labels.nlevels:
                        return {"key": key}
                    raise
+            except InvalidIndexError:
+               if not isinstance(labels, ABCMultiIndex):
+                    raise

I haven't run the test suite with this, nor looked at this closely enough to confirm that its the "correct" fix, but it does fix the example in the OP.

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jul 6, 2020

@jbrockmendel Thanks. I will create a test, try that fix and let you know if something else pops up

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Jul 6, 2020

@jbrockmendel OK, that seemed to fix it in #35140 . Thanks!

@jreback jreback added this to the 1.1 milestone Jul 6, 2020
@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jul 6, 2020
@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants