Consider a data frame. df
Note that the column object is a single-level MultiIndex.
midx = pd.MultiIndex.from_product([list('AB')])
df = pd.DataFrame(1, range(3), midx)
A B
0 1 1
1 1 1
2 1 1
Now when i reference the column 'A'
df.A
A
0 1
1 1
2 1
I get one frame of column data, not the series object that I was expecting. Therefore, I can refer to this column endlessly.
df.A.A.A.A.A
A
0 1
1 1
2 1
As another check, I used xs
df.xs('A', axis=1)
A
0 1
1 1
2 1
Same problem.
pd.IndexSlice?
df.loc[:, pd.IndexSlice['A']]
A
0 1
1 1
2 1
What about squeeze
df.A.squeeze()
0 1
1 1
2 1
Name: (A,), dtype: int64
This is not at all what I expected.
- What prevents this from becoming a series object with a name
'A'? - What is the most intuitive way to fix this?
- Is there a good reason we need one level
MultiIndex?
source
share