Skip to content

MultiIndex levels() and get_level_values() give different results #16160

Closed
@miishke

Description

@miishke

Code Sample, a copy-pastable example if possible

import pandas as pd

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 
                         'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 
                         'Scouts', 'Scouts', 'Scouts'], 
            'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd', 
                        '1st', '1st', '2nd', '2nd'], 
            'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 
                     'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
            'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3], 
            'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}

df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
df_gbm = df.groupby(['regiment', 'company']).mean()
df_gbm_preTS_gt10 = df_gbm[df_gbm.preTestScore > 10]

df_gbm_preTS_gt10.index.levels[0].tolist()
df_gbm_preTS_gt10.index.get_level_values(0).unique().tolist()

Problem description

I was expecting MultiIndex.levels[0].tolist() to give the same results as MultiIndex.get_level_values(0).unique().tolist() but that is not the case.

df_gbm_preTS_gt10

preTestScore postTestScore
regiment company
Dragoons 2nd 27.5 75.5
Nighthawks 1st 14.0 59.5
2nd 16.5 59.5
df_gbm_preTS_gt10.index.levels[0].tolist()
['Dragoons', 'Nighthawks', 'Scouts']

df_gbm_preTS_gt10.index.get_level_values(0).unique().tolist()
['Dragoons', 'Nighthawks']

Expected Output

df_gbm_preTS_gt10.index.levels[0].tolist()
['Dragoons', 'Nighthawks']

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-20-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.12.1
scipy: 0.19.0
statsmodels: None
xarray: None
IPython: 6.0.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions