|
23 | 23 | from pandas.core.dtypes.missing import isna
|
24 | 24 |
|
25 | 25 | import pandas.core.algorithms as algos
|
| 26 | +from pandas.core.internals.blocks import make_block |
| 27 | +from pandas.core.internals.managers import BlockManager |
26 | 28 |
|
27 | 29 |
|
28 |
| -def get_mgr_concatenation_plan(mgr, indexers): |
| 30 | +def concatenate_block_managers( |
| 31 | + mgrs_indexers, axes, concat_axis: int, copy: bool |
| 32 | +) -> BlockManager: |
| 33 | + """ |
| 34 | + Concatenate block managers into one. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + mgrs_indexers : list of (BlockManager, {axis: indexer,...}) tuples |
| 39 | + axes : list of Index |
| 40 | + concat_axis : int |
| 41 | + copy : bool |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + BlockManager |
| 46 | + """ |
| 47 | + concat_plans = [ |
| 48 | + _get_mgr_concatenation_plan(mgr, indexers) for mgr, indexers in mgrs_indexers |
| 49 | + ] |
| 50 | + concat_plan = _combine_concat_plans(concat_plans, concat_axis) |
| 51 | + blocks = [] |
| 52 | + |
| 53 | + for placement, join_units in concat_plan: |
| 54 | + |
| 55 | + if len(join_units) == 1 and not join_units[0].indexers: |
| 56 | + b = join_units[0].block |
| 57 | + values = b.values |
| 58 | + if copy: |
| 59 | + values = values.copy() |
| 60 | + else: |
| 61 | + values = values.view() |
| 62 | + b = b.make_block_same_class(values, placement=placement) |
| 63 | + elif _is_uniform_join_units(join_units): |
| 64 | + b = join_units[0].block.concat_same_type([ju.block for ju in join_units]) |
| 65 | + b.mgr_locs = placement |
| 66 | + else: |
| 67 | + b = make_block( |
| 68 | + _concatenate_join_units(join_units, concat_axis, copy=copy), |
| 69 | + placement=placement, |
| 70 | + ) |
| 71 | + blocks.append(b) |
| 72 | + |
| 73 | + return BlockManager(blocks, axes) |
| 74 | + |
| 75 | + |
| 76 | +def _get_mgr_concatenation_plan(mgr, indexers): |
29 | 77 | """
|
30 | 78 | Construct concatenation plan for given block manager and indexers.
|
31 | 79 |
|
@@ -232,7 +280,7 @@ def get_reindexed_values(self, empty_dtype, upcasted_na):
|
232 | 280 | return values
|
233 | 281 |
|
234 | 282 |
|
235 |
| -def concatenate_join_units(join_units, concat_axis, copy): |
| 283 | +def _concatenate_join_units(join_units, concat_axis, copy): |
236 | 284 | """
|
237 | 285 | Concatenate values from several join units along selected axis.
|
238 | 286 | """
|
@@ -371,11 +419,11 @@ def _get_empty_dtype_and_na(join_units):
|
371 | 419 | raise AssertionError(msg)
|
372 | 420 |
|
373 | 421 |
|
374 |
| -def is_uniform_join_units(join_units) -> bool: |
| 422 | +def _is_uniform_join_units(join_units) -> bool: |
375 | 423 | """
|
376 | 424 | Check if the join units consist of blocks of uniform type that can
|
377 | 425 | be concatenated using Block.concat_same_type instead of the generic
|
378 |
| - concatenate_join_units (which uses `concat_compat`). |
| 426 | + _concatenate_join_units (which uses `concat_compat`). |
379 | 427 |
|
380 | 428 | """
|
381 | 429 | return (
|
@@ -429,7 +477,7 @@ def _trim_join_unit(join_unit, length):
|
429 | 477 | return JoinUnit(block=extra_block, indexers=extra_indexers, shape=extra_shape)
|
430 | 478 |
|
431 | 479 |
|
432 |
| -def combine_concat_plans(plans, concat_axis): |
| 480 | +def _combine_concat_plans(plans, concat_axis): |
433 | 481 | """
|
434 | 482 | Combine multiple concatenation plans into one.
|
435 | 483 |
|
|
0 commit comments