Skip to content

Commit 326af92

Browse files
jbrockmendelfeefladder
authored andcommitted
REF: de-duplicate consolidation methods (pandas-dev#42822)
1 parent 8f2f14f commit 326af92

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

pandas/core/internals/array_manager.py

-12
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,6 @@ def set_axis(self, axis: int, new_labels: Index) -> None:
167167
axis = self._normalize_axis(axis)
168168
self._axes[axis] = new_labels
169169

170-
def consolidate(self: T) -> T:
171-
return self
172-
173-
def is_consolidated(self) -> bool:
174-
return True
175-
176-
def _consolidate_inplace(self) -> None:
177-
pass
178-
179170
def get_dtypes(self):
180171
return np.array([arr.dtype for arr in self.arrays], dtype="object")
181172

@@ -1262,9 +1253,6 @@ def _can_hold_na(self) -> bool:
12621253
def is_single_block(self) -> bool:
12631254
return True
12641255

1265-
def _consolidate_check(self):
1266-
pass
1267-
12681256
def fast_xs(self, loc: int) -> ArrayLike:
12691257
raise NotImplementedError("Use series._values[loc] instead")
12701258

pandas/core/internals/base.py

+16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class DataManager(PandasObject):
3333
def items(self) -> Index:
3434
raise AbstractMethodError(self)
3535

36+
@final
3637
def __len__(self) -> int:
3738
return len(self.items)
3839

@@ -105,6 +106,7 @@ def _equal_values(self: T, other: T) -> bool:
105106
"""
106107
raise AbstractMethodError(self)
107108

109+
@final
108110
def equals(self, other: object) -> bool:
109111
"""
110112
Implementation for DataFrame.equals
@@ -129,13 +131,27 @@ def apply(
129131
) -> T:
130132
raise AbstractMethodError(self)
131133

134+
@final
132135
def isna(self: T, func) -> T:
133136
return self.apply("apply", func=func)
134137

138+
# --------------------------------------------------------------------
139+
# Consolidation: No-ops for all but BlockManager
140+
141+
def is_consolidated(self) -> bool:
142+
return True
143+
144+
def consolidate(self: T) -> T:
145+
return self
146+
147+
def _consolidate_inplace(self) -> None:
148+
return
149+
135150

136151
class SingleDataManager(DataManager):
137152
ndim = 1
138153

154+
@final
139155
@property
140156
def array(self):
141157
"""

pandas/core/internals/managers.py

+23-29
Original file line numberDiff line numberDiff line change
@@ -465,19 +465,6 @@ def to_native_types(self: T, **kwargs) -> T:
465465
"""
466466
return self.apply("to_native_types", **kwargs)
467467

468-
def is_consolidated(self) -> bool:
469-
"""
470-
Return True if more than one block with the same dtype
471-
"""
472-
if not self._known_consolidated:
473-
self._consolidate_check()
474-
return self._is_consolidated
475-
476-
def _consolidate_check(self) -> None:
477-
dtypes = [blk.dtype for blk in self.blocks if blk._can_consolidate]
478-
self._is_consolidated = len(dtypes) == len(set(dtypes))
479-
self._known_consolidated = True
480-
481468
@property
482469
def is_numeric_mixed_type(self) -> bool:
483470
return all(block.is_numeric for block in self.blocks)
@@ -623,13 +610,6 @@ def consolidate(self: T) -> T:
623610
bm._consolidate_inplace()
624611
return bm
625612

626-
def _consolidate_inplace(self) -> None:
627-
if not self.is_consolidated():
628-
self.blocks = tuple(_consolidate(self.blocks))
629-
self._is_consolidated = True
630-
self._known_consolidated = True
631-
self._rebuild_blknos_and_blklocs()
632-
633613
def reindex_indexer(
634614
self: T,
635615
new_axis: Index,
@@ -1551,6 +1531,29 @@ def _interleave(
15511531

15521532
return result
15531533

1534+
# ----------------------------------------------------------------
1535+
# Consolidation
1536+
1537+
def is_consolidated(self) -> bool:
1538+
"""
1539+
Return True if more than one block with the same dtype
1540+
"""
1541+
if not self._known_consolidated:
1542+
self._consolidate_check()
1543+
return self._is_consolidated
1544+
1545+
def _consolidate_check(self) -> None:
1546+
dtypes = [blk.dtype for blk in self.blocks if blk._can_consolidate]
1547+
self._is_consolidated = len(dtypes) == len(set(dtypes))
1548+
self._known_consolidated = True
1549+
1550+
def _consolidate_inplace(self) -> None:
1551+
if not self.is_consolidated():
1552+
self.blocks = tuple(_consolidate(self.blocks))
1553+
self._is_consolidated = True
1554+
self._known_consolidated = True
1555+
self._rebuild_blknos_and_blklocs()
1556+
15541557

15551558
class SingleBlockManager(BaseBlockManager, SingleDataManager):
15561559
"""manage a single block with"""
@@ -1710,15 +1713,6 @@ def array_values(self):
17101713
def _can_hold_na(self) -> bool:
17111714
return self._block._can_hold_na
17121715

1713-
def is_consolidated(self) -> bool:
1714-
return True
1715-
1716-
def _consolidate_check(self):
1717-
pass
1718-
1719-
def _consolidate_inplace(self):
1720-
pass
1721-
17221716
def idelete(self, indexer) -> SingleBlockManager:
17231717
"""
17241718
Delete single location from SingleBlockManager.

0 commit comments

Comments
 (0)