Skip to content

Commit ea98a29

Browse files
authored
REF: dont consolidate in is_mixed_type (#36873)
1 parent 24fef22 commit ea98a29

File tree

6 files changed

+11
-32
lines changed

6 files changed

+11
-32
lines changed

pandas/core/frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ def _is_homogeneous_type(self) -> bool:
631631
if self._mgr.any_extension_types:
632632
return len({block.dtype for block in self._mgr.blocks}) == 1
633633
else:
634-
# Note: consolidates inplace
635634
return not self._is_mixed_type
636635

637636
@property

pandas/core/generic.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -5472,8 +5472,15 @@ def _consolidate(self, inplace: bool_t = False):
54725472

54735473
@property
54745474
def _is_mixed_type(self) -> bool_t:
5475-
f = lambda: self._mgr.is_mixed_type
5476-
return self._protect_consolidate(f)
5475+
if len(self._mgr.blocks) == 1:
5476+
return False
5477+
5478+
if self._mgr.any_extension_types:
5479+
# Even if they have the same dtype, we cant consolidate them,
5480+
# so we pretend this is "mixed'"
5481+
return True
5482+
5483+
return self.dtypes.nunique() > 1
54775484

54785485
def _check_inplace_setting(self, value) -> bool_t:
54795486
""" check whether we allow in-place setting with this type of value """
@@ -6253,8 +6260,7 @@ def fillna(
62536260
axis = self._get_axis_number(axis)
62546261

62556262
if value is None:
6256-
6257-
if self._is_mixed_type and axis == 1:
6263+
if len(self._mgr.blocks) > 1 and axis == 1:
62586264
if inplace:
62596265
raise NotImplementedError()
62606266
result = self.T.fillna(method=method, limit=limit).T

pandas/core/indexing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,7 @@ def _setitem_with_indexer(self, indexer, value):
15351535
info_axis = self.obj._info_axis_number
15361536

15371537
# maybe partial set
1538-
# _is_mixed_type has the side effect of consolidating in-place
1539-
take_split_path = self.obj._is_mixed_type
1538+
take_split_path = len(self.obj._mgr.blocks) > 1
15401539

15411540
# if there is only one block/type, still have to take split path
15421541
# unless the block is one-dimensional or it can hold the value

pandas/core/internals/managers.py

-6
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,6 @@ def _consolidate_check(self) -> None:
655655
self._is_consolidated = len(dtypes) == len(set(dtypes))
656656
self._known_consolidated = True
657657

658-
@property
659-
def is_mixed_type(self) -> bool:
660-
# Warning, consolidation needs to get checked upstairs
661-
self._consolidate_inplace()
662-
return len(self.blocks) > 1
663-
664658
@property
665659
def is_numeric_mixed_type(self) -> bool:
666660
return all(block.is_numeric for block in self.blocks)

pandas/tests/frame/test_dtypes.py

-12
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,6 @@ def test_constructor_list_str_na(self, string_dtype):
230230
def test_is_homogeneous_type(self, data, expected):
231231
assert data._is_homogeneous_type is expected
232232

233-
def test_is_homogeneous_type_clears_cache(self):
234-
ser = pd.Series([1, 2, 3])
235-
df = ser.to_frame("A")
236-
df["B"] = ser
237-
238-
assert len(df._mgr.blocks) == 2
239-
240-
a = df["B"] # caches lookup
241-
df._is_homogeneous_type # _should_ clear cache
242-
assert len(df._mgr.blocks) == 1
243-
assert df["B"] is not a
244-
245233
def test_asarray_homogenous(self):
246234
df = pd.DataFrame({"A": pd.Categorical([1, 2]), "B": pd.Categorical([1, 2])})
247235
result = np.asarray(df)

pandas/tests/internals/test_internals.py

-7
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,6 @@ def test_attrs(self):
272272
assert mgr.nblocks == 2
273273
assert len(mgr) == 6
274274

275-
def test_is_mixed_dtype(self):
276-
assert not create_mgr("a,b:f8").is_mixed_type
277-
assert not create_mgr("a:f8-1; b:f8-2").is_mixed_type
278-
279-
assert create_mgr("a,b:f8; c,d: f4").is_mixed_type
280-
assert create_mgr("a,b:f8; c,d: object").is_mixed_type
281-
282275
def test_duplicate_ref_loc_failure(self):
283276
tmp_mgr = create_mgr("a:bool; a: f8")
284277

0 commit comments

Comments
 (0)