Skip to content

BUG: Result of pd.concat([], keys=) with identical key has trouble with MultiIndex .loc[]  #46519

Closed
@kwhkim

Description

@kwhkim

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

df1 = pd.DataFrame(
  {'name':["김희선","김희선", "박보검"],
  'date':pd.to_datetime(['2022-01-02', '2022-01-03', '2022-01-04']),
  'product':["바지","샴푸", "샴푸"]}  
)
df2 = pd.DataFrame(
  {'name' :["김희선","박보검"],
   'date':pd.to_datetime(['2022-04-01', '2022-04-15']),
   'product':["텔레비전","바지"]}  
)
df3 = pd.DataFrame(
  {"name":["비회원", "김수현"],
  "date":pd.to_datetime(['2022-05-01', '2022-05-25']),
  "product":["바지","텔레비전"]}
)
dat = pd.concat([df1, df2, df3], keys=['dfA', 'dfB', 'dfA'])
dat.loc[("dfA", 0),:]

dfAll = pd.DataFrame(
  {'name':["김희선","김희선", "박보검",
           "김희선","박보검", 
           "비회원","김수현"],
  'date':pd.to_datetime(['2022-01-02', '2022-01-03', '2022-01-04',
                        '2022-04-01', '2022-04-15',
                        '2022-05-01', '2022-05-25']),
  'product':["바지","샴푸", "샴푸",
            "텔레비전","바지",
            "바지","텔레비전"]}  ,
    index = [['dfA', 'dfA', 'dfA', 'dfB', 'dfB', 'dfA', 'dfA'],
            [0,1,2,0,1,0,1]]
)

Issue Description

The result from pd.concat([,,,], keys=[]) with identical key name does not allow multi-level indexing

Expected Behavior

dat.loc[("dfA", 0),:] should be

         	  name	date	product
dfA	0	김희선	2022-01-02	바지
            0	비회원	2022-05-01	바지

just like datAll.loc[("dfA", 0),:]
instead of generating KeyError: 'dfA'

or maybe prevent users from using pd.concat([], keys=[]) with identical key.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Korean_Korea.949

pandas : 1.4.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 60.9.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : 1.0.9
s3fs : None
scipy : 1.8.0
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions