What happens behind Pandas scenes that cause a drop in MultiIndex?

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?
+4
source share
1 answer

, .

def fix_single_level_multiindex(midx):
    return midx.get_level_values(0) if midx.nlevels == 1 else midx

def fix_single_level_multiindex(midx):
    return midx.levels[0][midx.labels[0]] if midx.nlevels == 1 else midx
0

Source: https://habr.com/ru/post/1695725/


All Articles