Skip to content

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

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
miishke opened this issue Apr 27, 2017 · 3 comments
Closed

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

miishke opened this issue Apr 27, 2017 · 3 comments
Labels

Comments

@miishke
Copy link

miishke commented Apr 27, 2017

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

@jreback
Copy link
Contributor

jreback commented Apr 27, 2017

@jreback jreback closed this as completed Apr 27, 2017
@jreback jreback added Duplicate Report Duplicate issue or pull request MultiIndex Usage Question labels Apr 27, 2017
@jreback jreback added this to the No action milestone Apr 27, 2017
@miishke
Copy link
Author

miishke commented Apr 27, 2017

OK, thanks. Should I make a PR for documenting this? Because this is the entry for pandas.MultiIndex.levels in the API documentation:

selection_001

@jreback
Copy link
Contributor

jreback commented Apr 27, 2017

additional documentation is always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants