Skip to content

MultiIndex-Columns Integer and String: Column level 1 not accessable after a using append multiple times #29699

Closed
@Alrik-star

Description

@Alrik-star

Code Sample, a copy-pastable example if possible

import pandas as pd
pd.show_versions()

# Create Multi-Index
df = pd.DataFrame({'col1':[1,2,3], 'col2':[11,12,13]})
df = pd.concat([df], keys=['multi'], names=['level1'], axis=1)


for i in range(100):
    df[i, 'colA'] = 10
    a = pd.DataFrame({'col1':[1,2,3], 'col2':[11,12,13]})
    a = pd.concat([a], keys=['multi'], names=['level1'], axis=1)
    df = df.append(a, ignore_index=True)
    print(df['multi'])

Problem description

Raises an exception "KeyError: 'multi' ", although this should perfectly work, as the key "Multi" exists, e.g when printing df.columns.

The most strange thing is:

  • The step in which this exception is raised depends on the Name of the key "Multi".. Sometimes 2nd loop, sometimes 6th,...
  • During the first loop it works perfectly and prints the df

However it has to do something with using "integer" and "string" column-names for level1 --- if i use an integer, e.g. -1 instead of "Multi", this seems to work.

Expected Output

It prints 100 times the dataframe

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.1.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : None.None

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 39.0.1
Cython : None
pytest : 5.2.2
hypothesis : None
sphinx : 2.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions