Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

T = pd.core.arrays.integer.Int8Dtype()

df = pd.DataFrame({
    ('a', 'x'): pd.Series(range(5), dtype=T),
    ('b', 'x'): pd.Series(range(5)),
})

dtypes = df.stack().dtypes

assert dtypes['a'] == T, dtypes['a']

Issue Description

Extension dtypes (including categoricals) are down-cast to object if they are part of a multi-column data frame with more than one label on the non-stacked level(s).

The function _stack_multi_columns contains a branch for a homogenous extension dtype frame. (https://github.com/pandas-dev/pandas/blob/4f0e22c1c6d9cebed26f45a9a5b873ebbe67cd17/pandas/core/reshape/reshape.py#L744,L745)

The task of this branch is to ensure that extension dtypes are preserved by the stacking.

There’s a bug that causes this branch to not be executed.

Expected Behavior

Expectation: Extension Dtypes should be preserved under stacking.

Installed Versions

Pandas 1.4

Comment From: mroeschke

Thanks for the report @ladidadadum. Happy to have your pull request https://github.com/ladidadadum/pandas/commit/430dd915a7094e0124f72ff27ae3aa17162f8b0f with an associated test.

Comment From: ladidadadum

Thank you @mroeschke for the reply.

I've added a simple test to a new file at extension/test_stack.py. Please let me know if I should - add more tests - move the test elsewhere

Also, I didn't see any best practice guidance around re-base before PR. Please let me know in case I should rebase.

This is my first public GitHub PR, and I hope that I've followed the protocol as good as possible, but please let me know if anything is missing.

Regards LD