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.

Option to suppress automatic conversion of tuples to MultiIndex

See original GitHub issue

Right now we have:

>>> pd.DataFrame({(1,2):pd.Series([2.3])}).columns
MultiIndex(levels=[[1], [2]],                     
           labels=[[0], [0]])                     
>>> pd.Index([(1,2)])
MultiIndex(levels=[[1], [2]],
           labels=[[0], [0]])

Could we have an option to suppress this behavior? One problem this causes is that the .rename method is not uniform and does not work if a tuple is silently converted:

>>> pd.DataFrame({(1,2):pd.Series([2.3])}).rename(columns={(1,2):(1,3)})
     1                                                                  
     2                                                                  
0  2.3                                                                  

To see why such behavior is problematic consider the following unintuitive example:

>>> a = ('a', 1)

>>> b = ('b', 2)

>>> pd.DataFrame({a:pd.Series([2,3])})
   a
   1
0  2
1  3

>>> pd.DataFrame({a:pd.Series([2,3])}).rename(columns={a:b})
   a
   1
0  2
1  3

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
htzhcommented, Dec 13, 2015

Thanks to all for the help. I think I will try to grok MultiIndex better and more effectively incorporate it into the processing pipeline sooner. I also realized that one way to achieve what I wanted is to first convert names of rows as a data column and change the names in the data column. Pandas offers convenient ways to then use the changed column as Index or MultiIndex.

@jreback I read the #4160 thread but it is not completely clear to me what the proposed API is. Is the proposal to make the following statement work for MultiIndex:

df.rename(columns={('abspx','red) : ('foo','orange')})

presumably moving the particular column from one part of the hierarchy to another? If that is the proposal that is also what I requested earlier: “rename method (for DataFrame or Series) accepts tuple as specifying MultiIndex instead of tuple index just like in operator does.”

I am not yet familiar with pandas source but I will keep that in mind.

1reaction
htzhcommented, Dec 10, 2015

I accept that tuple is not desirable. My problem is that during data cleaning phase I need to rename index (column or row) at a few places. However the conversion of tuple to MultiIndex is not consistent for row index:

>>> pd.DataFrame({'a':pd.Series([2, 3], index=[(1,1), (2,2)])})
        a
(1, 1)  2
(2, 2)  3

And as rename semantics is not uniform between Index and MultiIndex, it would be nice to have consistent behavior here:

  • tuple row index converts to MultiIndex
  • rename method (for DataFrame or Series) accepts tuple as specifying MultiIndex instead of tuple index just like in operator does.
Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.MultiIndex.from_tuples — pandas 1.5.2 documentation
Convert list of tuples to MultiIndex. Parameters. tupleslist / sequence of tuple-likes. Each tuple is the index of one row/column. sortorderint or None....
Read more >
Pandas multi index partial selection using list of tuples
Now If i try to index using a list of tuples using only two levels. df.loc[[('c1', 'd1'), ('c2', 'd2')]]. I get the following...
Read more >
Confused by Multi-Index in Pandas? 9 Essential Operations to ...
Converting MultiIndex to Regular Index. In the example DataFrame (i.e., df_mean ), the multi-index is created automatically as a result of using ...
Read more >
DataFrame Schemas - pandera
SchemaError: Error while coercing 'column1' to type int64: Cannot convert ... multi-index columns follows the pandas syntax of specifying tuples for each ...
Read more >
Pandas reset index - How to reset the index and convert the ...
If the DataFrame has a MultiIndex, this method can remove one or more levels. Parameters. level: int, str, tuple or list, (default None) ......
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