diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d12ebeafe8510..d868f503b3ea0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -611,7 +611,8 @@ def _is_homogeneous_type(self) -> bool: if self._mgr.any_extension_types: return len({block.dtype for block in self._mgr.blocks}) == 1 else: - return not self._mgr.is_mixed_type + # Note: consolidates inplace + return not self._is_mixed_type @property def _can_fast_transpose(self) -> bool: diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 9c415564fd99a..f3e3ef9bae5c6 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -233,6 +233,18 @@ def test_constructor_list_str_na(self, string_dtype): def test_is_homogeneous_type(self, data, expected): assert data._is_homogeneous_type is expected + def test_is_homogeneous_type_clears_cache(self): + ser = pd.Series([1, 2, 3]) + df = ser.to_frame("A") + df["B"] = ser + + assert len(df._mgr.blocks) == 2 + + a = df["B"] # caches lookup + df._is_homogeneous_type # _should_ clear cache + assert len(df._mgr.blocks) == 1 + assert df["B"] is not a + def test_asarray_homogenous(self): df = pd.DataFrame({"A": pd.Categorical([1, 2]), "B": pd.Categorical([1, 2])}) result = np.asarray(df)