<h4 dir="auto">Code Sample</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
import io

text = &quot;&quot;&quot;
A B C
1 1 1
1 2 2
2 1 3
2 2 4
&quot;&quot;&quot;
df = pd.read_csv(io.StringIO(text), delimiter = ' ')
# Note the output of get_level_values is the list of all values [1, 1, 2, 2]
print(df.index.get_level_values('A').tolist())
df.set_index(['A','B'], inplace = True)
df.index.set_levels(df.index.get_level_values('A').map(lambda x: x * 2), level='A', inplace=True)
print(df.index.get_level_values('A').tolist())
# outputs [2, 2, 2, 2]"><pre class="notranslate"><code class="notranslate">import pandas as pd
import io

text = """
A B C
1 1 1
1 2 2
2 1 3
2 2 4
"""
df = pd.read_csv(io.StringIO(text), delimiter = ' ')
# Note the output of get_level_values is the list of all values [1, 1, 2, 2]
print(df.index.get_level_values('A').tolist())
df.set_index(['A','B'], inplace = True)
df.index.set_levels(df.index.get_level_values('A').map(lambda x: x * 2), level='A', inplace=True)
print(df.index.get_level_values('A').tolist())
# outputs [2, 2, 2, 2]
</code></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto"><code class="notranslate">[2, 2, 4, 4]</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# If I re-order the input as:
text = &quot;&quot;&quot;
A B C
1 1 1
2 1 2
1 2 3
2 2 4
&quot;&quot;&quot;
# it works as I expected giving [2, 4, 2, 4]"><pre class="notranslate"><code class="notranslate"># If I re-order the input as:
text = """
A B C
1 1 1
2 1 2
1 2 3
2 2 4
"""
# it works as I expected giving [2, 4, 2, 4]
</code></pre></div>
<p dir="auto">Is this a bug or expected behaviour?</p>
<h4 dir="auto">output of <code class="notranslate">pd.show_versions()</code></h4>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.5.1.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None</p>
<p dir="auto">pandas: 0.18.1<br>
nose: 1.3.7<br>
pip: 8.1.1<br>
setuptools: 20.3<br>
Cython: 0.23.4<br>
numpy: 1.10.4<br>
scipy: 0.17.0<br>
statsmodels: 0.6.1<br>
xarray: None<br>
IPython: 4.1.2<br>
sphinx: 1.3.1<br>
patsy: 0.4.0<br>
dateutil: 2.5.1<br>
pytz: 2016.2<br>
blosc: None<br>
bottleneck: 1.0.0<br>
tables: 3.2.2<br>
numexpr: 2.5<br>
matplotlib: 1.5.1<br>
openpyxl: 2.3.2<br>
xlrd: 0.9.4<br>
xlwt: 1.0.0<br>
xlsxwriter: 0.8.4<br>
lxml: 3.6.0<br>
bs4: 4.4.1<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.0.12<br>
pymysql: 0.7.5.None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: 2.39.0<br>
pandas_datareader: None</p>
<p dir="auto">Small example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [87]: df = pd.DataFrame(columns=pd.MultiIndex.from_product([['A','B'],['a','b']]))"><pre class="notranslate"><code class="notranslate">In [87]: df = pd.DataFrame(columns=pd.MultiIndex.from_product([['A','B'],['a','b']]))
</code></pre></div>
<p dir="auto">Setting the levels with an incorrect levels (too short), fails (correctly of course):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [92]: df.columns.set_levels(['c'], level=1)
...
ValueError: On level 1, label max (1) &gt;= length of level  (1). NOTE: this index
is in an inconsistent state"><pre class="notranslate"><code class="notranslate">In [92]: df.columns.set_levels(['c'], level=1)
...
ValueError: On level 1, label max (1) &gt;= length of level  (1). NOTE: this index
is in an inconsistent state
</code></pre></div>
<p dir="auto">When using <code class="notranslate">inplace=True</code>, this also gives the same error message, but the index <em>is</em> actually changed to this inconsistent state:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [93]: df.columns.set_levels(['c'], level=1, inplace=True)
...
ValueError: On level 1, label max (1) &gt;= length of level  (1). NOTE: this index
is in an inconsistent state

In [94]: df.columns
Out[94]:
MultiIndex(levels=[[u'A', u'B'], [u'c']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"><pre class="notranslate"><code class="notranslate">In [93]: df.columns.set_levels(['c'], level=1, inplace=True)
...
ValueError: On level 1, label max (1) &gt;= length of level  (1). NOTE: this index
is in an inconsistent state

In [94]: df.columns
Out[94]:
MultiIndex(levels=[[u'A', u'B'], [u'c']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
</code></pre></div>
<p dir="auto">Just showing this <code class="notranslate">df</code> in the console did crash my session in the case above, in the example from <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="166796174" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/13741" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/13741/hovercard" href="https://github.com/pandas-dev/pandas/issues/13741">#13741</a>, it gave the error "IndexError: index 1 is out of bounds for axis 0 with size 1"</p>