<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="s = pd.Series([0], index=pd.MultiIndex.from_tuples([('a', 1)]))

# This works:
s.rename(lambda x: x)

# This fails:
s.iloc[0:0].rename(lambda x: x)"><pre class="notranslate"><span class="pl-s1">s</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>([<span class="pl-c1">0</span>], <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_tuples</span>([(<span class="pl-s">'a'</span>, <span class="pl-c1">1</span>)]))

<span class="pl-c"># This works:</span>
<span class="pl-s1">s</span>.<span class="pl-en">rename</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">x</span>)

<span class="pl-c"># This fails:</span>
<span class="pl-s1">s</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>:<span class="pl-c1">0</span>].<span class="pl-en">rename</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">x</span>)</pre></div>
<p dir="auto">Renaming an empty index should not throw an error. It should just return the original empty index. It appears to be failing because <code class="notranslate">_transform_index</code> calls <code class="notranslate">MultiIndex.from_tuples</code>, which doesn't accept an empty list of tuples.</p>
<p dir="auto">I would imagine that this could instead call <code class="notranslate">from_arrays</code>, which <code class="notranslate">from_tuples</code> calls anyway, or it could check for the special case of an empty index.</p>
<p dir="auto">xref <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="174840894" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/14139" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/14139/hovercard" href="https://github.com/pandas-dev/pandas/issues/14139">#14139</a> for empty MI</p>
<p dir="auto">Hi everybody,</p>
<p dir="auto">in the current version renaming of MultiIndex DataFrames does not work. Lets take the following example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import datetime as DT
import pandas as pd
df = pd.DataFrame({
'Branch' : 'A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3],
'Date' : [
    DT.datetime(2013,9,1,13,0),
    DT.datetime(2013,9,1,13,5),
    DT.datetime(2013,10,1,20,0),
    DT.datetime(2013,10,3,10,0),
    DT.datetime(2013,12,2,12,0),                                      
    DT.datetime(2013,12,2,14,0),
    ]})"><pre class="notranslate"><code class="notranslate">import datetime as DT
import pandas as pd
df = pd.DataFrame({
'Branch' : 'A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3],
'Date' : [
    DT.datetime(2013,9,1,13,0),
    DT.datetime(2013,9,1,13,5),
    DT.datetime(2013,10,1,20,0),
    DT.datetime(2013,10,3,10,0),
    DT.datetime(2013,12,2,12,0),                                      
    DT.datetime(2013,12,2,14,0),
    ]})
</code></pre></div>
<p dir="auto">and the following query:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])"><pre class="notranslate"><code class="notranslate">test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])
</code></pre></div>
<p dir="auto">Now, the following renaming does not work</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)"><pre class="notranslate"><code class="notranslate">test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)
</code></pre></div>
<p dir="auto">Thanks in advance</p>
<p dir="auto">Andy</p>