Option to suppress automatic conversion of tuples to MultiIndex
See original GitHub issueRight 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:
- Created 8 years ago
- Reactions:1
- Comments:14 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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 (forDataFrame
orSeries
) accepts tuple as specifyingMultiIndex
instead of tuple index just likein
operator does.”I am not yet familiar with pandas source but I will keep that in mind.
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:And as
rename
semantics is not uniform betweenIndex
andMultiIndex
, it would be nice to have consistent behavior here:rename
method (for DataFrame or Series) accepts tuple as specifyingMultiIndex
instead of tuple index just likein
operator does.