question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH/BUG: Rename of MultiIndex DataFrames does not work

See original GitHub issue

xref #14139 for empty MI

Hi everybody,

in the current version renaming of MultiIndex DataFrames does not work. Lets take the following example:

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),
    ]})

and the following query:

test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])

Now, the following renaming does not work

test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)

Thanks in advance

Andy

Issue Analytics

  • State:open
  • Created 10 years ago
  • Comments:25 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
cklbcommented, Aug 23, 2021

Just stumbled over this problem. The delicate part is that the function will always return successfully even if errors="raise" is passed.

Thanks to normanius for the workaround, if a fix is not that easy, maybe the docstring could be extended with a warning?

1reaction
normaniuscommented, Nov 26, 2020

Just sharing a workaround: renaming of tuples works for flattened indices.

df = pd.DataFrame([[1,2,3],[3,4,5],[5,6,7], [7,8,9]])
df.columns = pd.MultiIndex.from_tuples([('i','a'),('i','b'),('ii','a')])

# Alternative 1
df.columns = df.columns.to_flat_index()
df = df.rename(columns={('i','b'):('i','c')})
df.columns = pd.MultiIndex.from_tuples(df.columns)

# Alternative 2
i = df.columns.get_loc(('i','b'))
cols = df.columns.to_flat_index()
cols[i] = ('i','c')
df.columns = pd.MultiIndex.from_tuples(cols)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Renaming MultiIndex columns is not working
Try using rename with a level argument - df = df.rename(columns='Sensor {}'.format , level=1). Thanks to Zero for the shorthand improvement.
Read more >
MultiIndex / advanced indexing
The rename() method is used to rename the labels of a MultiIndex , and is typically used to rename the columns of a...
Read more >
How do I use the MultiIndex in pandas? - YouTube
One of the most powerful features in pandas is multi-level indexing (or "hierarchical indexing"), which allows you to add extra dimensions ...
Read more >
Turn Pandas Multi-Index into column
A multi-index dataframe has multi-level, or hierarchical indexing. ... Here, we can see that only indx1 is reset becomes a column, not both ......
Read more >
How to Rename Multi index columns in Pandas Dataframe
Pandas dataframe is a powerful two dimensional data structure which allows you to store data in rows... Tagged with python, pandas, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found