Skip to content

Commit 7dc86cc

Browse files
authored
BUG: clear cache on DataFrame._is_homogeneous_dtype (#34937)
1 parent db48799 commit 7dc86cc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ def _is_homogeneous_type(self) -> bool:
611611
if self._mgr.any_extension_types:
612612
return len({block.dtype for block in self._mgr.blocks}) == 1
613613
else:
614-
return not self._mgr.is_mixed_type
614+
# Note: consolidates inplace
615+
return not self._is_mixed_type
615616

616617
@property
617618
def _can_fast_transpose(self) -> bool:

pandas/tests/frame/test_dtypes.py

+12
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ def test_constructor_list_str_na(self, string_dtype):
233233
def test_is_homogeneous_type(self, data, expected):
234234
assert data._is_homogeneous_type is expected
235235

236+
def test_is_homogeneous_type_clears_cache(self):
237+
ser = pd.Series([1, 2, 3])
238+
df = ser.to_frame("A")
239+
df["B"] = ser
240+
241+
assert len(df._mgr.blocks) == 2
242+
243+
a = df["B"] # caches lookup
244+
df._is_homogeneous_type # _should_ clear cache
245+
assert len(df._mgr.blocks) == 1
246+
assert df["B"] is not a
247+
236248
def test_asarray_homogenous(self):
237249
df = pd.DataFrame({"A": pd.Categorical([1, 2]), "B": pd.Categorical([1, 2])})
238250
result = np.asarray(df)

0 commit comments

Comments
 (0)