Skip to content

Commit bb9a985

Browse files
authored
REF: remove JoinUnit.shape (pandas-dev#43651)
1 parent 12f0b6a commit bb9a985

File tree

1 file changed

+16
-40
lines changed

1 file changed

+16
-40
lines changed

pandas/core/internals/concat.py

+16-40
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ def concatenate_managers(
212212
for placement, join_units in concat_plan:
213213
unit = join_units[0]
214214
blk = unit.block
215+
# Assertion disabled for performance
216+
# assert len(join_units) == len(mgrs_indexers)
215217

216218
if len(join_units) == 1:
217219
values = blk.values
@@ -329,27 +331,20 @@ def _get_mgr_concatenation_plan(mgr: BlockManager):
329331
plan : list of (BlockPlacement, JoinUnit) tuples
330332
331333
"""
332-
# Calculate post-reindex shape , save for item axis which will be separate
333-
# for each block anyway.
334-
mgr_shape_list = list(mgr.shape)
335-
mgr_shape = tuple(mgr_shape_list)
336334

337335
if mgr.is_single_block:
338336
blk = mgr.blocks[0]
339-
return [(blk.mgr_locs, JoinUnit(blk, mgr_shape))]
337+
return [(blk.mgr_locs, JoinUnit(blk))]
340338

341339
blknos = mgr.blknos
342340
blklocs = mgr.blklocs
343341

344342
plan = []
345343
for blkno, placements in libinternals.get_blkno_placements(blknos, group=False):
346344

347-
assert placements.is_slice_like
348-
assert blkno != -1
349-
350-
shape_list = list(mgr_shape)
351-
shape_list[0] = len(placements)
352-
shape = tuple(shape_list)
345+
# Assertions disabled for performance; these should always hold
346+
# assert placements.is_slice_like
347+
# assert blkno != -1
353348

354349
blk = mgr.blocks[blkno]
355350
ax0_blk_indexer = blklocs[placements.indexer]
@@ -379,19 +374,16 @@ def _get_mgr_concatenation_plan(mgr: BlockManager):
379374

380375
# Assertions disabled for performance
381376
# assert blk._mgr_locs.as_slice == placements.as_slice
382-
# assert blk.shape[0] == shape[0]
383-
unit = JoinUnit(blk, shape)
377+
unit = JoinUnit(blk)
384378

385379
plan.append((placements, unit))
386380

387381
return plan
388382

389383

390384
class JoinUnit:
391-
def __init__(self, block: Block, shape: Shape):
392-
# Passing shape explicitly is required for cases when block is None.
385+
def __init__(self, block: Block):
393386
self.block = block
394-
self.shape = shape
395387

396388
def __repr__(self) -> str:
397389
return f"{type(self).__name__}({repr(self.block)})"
@@ -404,22 +396,11 @@ def is_na(self) -> bool:
404396
return False
405397

406398
def get_reindexed_values(self, empty_dtype: DtypeObj) -> ArrayLike:
407-
values: ArrayLike
408-
409399
if self.is_na:
410-
return make_na_array(empty_dtype, self.shape)
400+
return make_na_array(empty_dtype, self.block.shape)
411401

412402
else:
413-
414-
if not self.block._can_consolidate:
415-
# preserve these for validation in concat_compat
416-
return self.block.values
417-
418-
# No dtype upcasting is done here, it will be performed during
419-
# concatenation itself.
420-
values = self.block.values
421-
422-
return values
403+
return self.block.values
423404

424405

425406
def make_na_array(dtype: DtypeObj, shape: Shape) -> ArrayLike:
@@ -558,6 +539,9 @@ def _is_uniform_join_units(join_units: list[JoinUnit]) -> bool:
558539
first = join_units[0].block
559540
if first.dtype.kind == "V":
560541
return False
542+
elif len(join_units) == 1:
543+
# only use this path when there is something to concatenate
544+
return False
561545
return (
562546
# exclude cases where a) ju.block is None or b) we have e.g. Int64+int64
563547
all(type(ju.block) is type(first) for ju in join_units)
@@ -570,13 +554,8 @@ def _is_uniform_join_units(join_units: list[JoinUnit]) -> bool:
570554
or ju.block.dtype.kind in ["b", "i", "u"]
571555
for ju in join_units
572556
)
573-
and
574-
# no blocks that would get missing values (can lead to type upcasts)
575-
# unless we're an extension dtype.
576-
all(not ju.is_na or ju.block.is_extension for ju in join_units)
577-
and
578-
# only use this path when there is something to concatenate
579-
len(join_units) > 1
557+
# this also precludes any blocks with dtype.kind == "V", since
558+
# we excluded that case for `first` above.
580559
)
581560

582561

@@ -598,10 +577,7 @@ def _trim_join_unit(join_unit: JoinUnit, length: int) -> JoinUnit:
598577
extra_block = join_unit.block.getitem_block(slice(length, None))
599578
join_unit.block = join_unit.block.getitem_block(slice(length))
600579

601-
extra_shape = (join_unit.shape[0] - length,) + join_unit.shape[1:]
602-
join_unit.shape = (length,) + join_unit.shape[1:]
603-
604-
return JoinUnit(block=extra_block, shape=extra_shape)
580+
return JoinUnit(block=extra_block)
605581

606582

607583
def _combine_concat_plans(plans):

0 commit comments

Comments
 (0)