Skip to content

Commit 1bb5417

Browse files
authored
CLN: remove Block.merge (#33240)
1 parent 23d8051 commit 1bb5417

File tree

3 files changed

+29
-46
lines changed

3 files changed

+29
-46
lines changed

pandas/core/internals/blocks.py

-29
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ def shape(self):
307307
def dtype(self):
308308
return self.values.dtype
309309

310-
def merge(self, other):
311-
return _merge_blocks([self, other])
312-
313310
def concat_same_type(self, to_concat):
314311
"""
315312
Concatenate list of single blocks of the same type.
@@ -2883,32 +2880,6 @@ def _block_shape(values, ndim=1, shape=None):
28832880
return values
28842881

28852882

2886-
def _merge_blocks(blocks, dtype=None, _can_consolidate=True):
2887-
2888-
if len(blocks) == 1:
2889-
return blocks[0]
2890-
2891-
if _can_consolidate:
2892-
2893-
if dtype is None:
2894-
if len({b.dtype for b in blocks}) != 1:
2895-
raise AssertionError("_merge_blocks are invalid!")
2896-
2897-
# FIXME: optimization potential in case all mgrs contain slices and
2898-
# combination of those slices is a slice, too.
2899-
new_mgr_locs = np.concatenate([b.mgr_locs.as_array for b in blocks])
2900-
new_values = np.vstack([b.values for b in blocks])
2901-
2902-
argsort = np.argsort(new_mgr_locs)
2903-
new_values = new_values[argsort]
2904-
new_mgr_locs = new_mgr_locs[argsort]
2905-
2906-
return make_block(new_values, placement=new_mgr_locs)
2907-
2908-
# no merge
2909-
return blocks
2910-
2911-
29122883
def _safe_reshape(arr, new_shape):
29132884
"""
29142885
If possible, reshape `arr` to have shape `new_shape`,

pandas/core/internals/managers.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
ExtensionBlock,
4444
ObjectValuesExtensionBlock,
4545
_extend_blocks,
46-
_merge_blocks,
4746
_safe_reshape,
4847
get_block_type,
4948
make_block,
@@ -1891,12 +1890,40 @@ def _consolidate(blocks):
18911890
new_blocks = []
18921891
for (_can_consolidate, dtype), group_blocks in grouper:
18931892
merged_blocks = _merge_blocks(
1894-
list(group_blocks), dtype=dtype, _can_consolidate=_can_consolidate
1893+
list(group_blocks), dtype=dtype, can_consolidate=_can_consolidate
18951894
)
18961895
new_blocks = _extend_blocks(merged_blocks, new_blocks)
18971896
return new_blocks
18981897

18991898

1899+
def _merge_blocks(
1900+
blocks: List[Block], dtype: DtypeObj, can_consolidate: bool
1901+
) -> List[Block]:
1902+
1903+
if len(blocks) == 1:
1904+
return blocks
1905+
1906+
if can_consolidate:
1907+
1908+
if dtype is None:
1909+
if len({b.dtype for b in blocks}) != 1:
1910+
raise AssertionError("_merge_blocks are invalid!")
1911+
1912+
# TODO: optimization potential in case all mgrs contain slices and
1913+
# combination of those slices is a slice, too.
1914+
new_mgr_locs = np.concatenate([b.mgr_locs.as_array for b in blocks])
1915+
new_values = np.vstack([b.values for b in blocks])
1916+
1917+
argsort = np.argsort(new_mgr_locs)
1918+
new_values = new_values[argsort]
1919+
new_mgr_locs = new_mgr_locs[argsort]
1920+
1921+
return [make_block(new_values, placement=new_mgr_locs)]
1922+
1923+
# can't consolidate --> no merge
1924+
return blocks
1925+
1926+
19001927
def _compare_or_regex_search(a, b, regex=False):
19011928
"""
19021929
Compare two array_like inputs of the same shape or two scalar values

pandas/tests/internals/test_internals.py

-15
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,6 @@ def test_attrs(self):
232232
assert self.fblock.dtype == self.fblock.values.dtype
233233
assert len(self.fblock) == len(self.fblock.values)
234234

235-
def test_merge(self):
236-
avals = tm.randn(2, 10)
237-
bvals = tm.randn(2, 10)
238-
239-
ref_cols = Index(["e", "a", "b", "d", "f"])
240-
241-
ablock = make_block(avals, ref_cols.get_indexer(["e", "b"]))
242-
bblock = make_block(bvals, ref_cols.get_indexer(["a", "d"]))
243-
merged = ablock.merge(bblock)
244-
tm.assert_numpy_array_equal(
245-
merged.mgr_locs.as_array, np.array([0, 1, 2, 3], dtype=np.int64)
246-
)
247-
tm.assert_numpy_array_equal(merged.values[[0, 2]], np.array(avals))
248-
tm.assert_numpy_array_equal(merged.values[[1, 3]], np.array(bvals))
249-
250235
def test_copy(self):
251236
cop = self.fblock.copy()
252237
assert cop is not self.fblock

0 commit comments

Comments
 (0)