Skip to content

DataFrame constructor with MultiIndex DataFrame raises ValueError #26459

Closed
@simonjayhawkins

Description

@simonjayhawkins

xref #26451

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(3, 3),
               columns=[['i', 'i', 'j'], ['A', 'A', 'B']],
               index=[['i', 'i', 'j'], ['X', 'X', 'Y']])
pd.DataFrame(df.iloc[[0, 1]], index=['X', 'X'])

Problem description

The error message is self-explanatory, but this case should work?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\frame.py", line 389, in __init__
    dtype=dtype, copy=copy)
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\generic.py", line 147, in _init_mg
r
    copy=False)
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\internals\managers.py", line 1189,
 in reindex_axis
    limit=limit)
  File "C:\Users\simon\OneDrive\code\pandas-simonjayhawkins\pandas\core\indexes\multi.py", line 2245, in r
eindex
    raise ValueError("cannot handle a non-unique multi-index!")
ValueError: cannot handle a non-unique multi-index!

Expected Output

          i                   j
          A         A         B
X  0.097722  0.726944  2.751392
X  0.396623 -0.564745 -0.153548

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 5a286e4
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: None.None

pandas: 0.25.0.dev0+584.g5a286e458
pytest: 4.4.2
pip: 19.1.1
setuptools: 40.6.3
Cython: 0.29.7
numpy: 1.16.3
scipy: 1.2.1
pyarrow: 0.11.1
xarray: 0.12.1
IPython: 7.5.0
sphinx: 2.0.1
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: 1.2.1
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml.etree: 4.3.3
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.3
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: 0.2.1
fastparquet: 0.3.0
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions