diff --git a/pandas/core/internals.py b/pandas/core/internals.py index f6c5ecbca81ef..045580d393b26 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -5182,7 +5182,15 @@ def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy): for placement, join_units in concat_plan: - if is_uniform_join_units(join_units): + if len(join_units) == 1 and not join_units[0].indexers: + b = join_units[0].block + values = b.values + if copy: + values = values.copy() + elif not copy: + values = values.view() + b = b.make_block_same_class(values, placement=placement) + elif is_uniform_join_units(join_units): b = join_units[0].block.concat_same_type( [ju.block for ju in join_units], placement=placement) else: diff --git a/pandas/tests/internals/test_external_block.py b/pandas/tests/internals/test_external_block.py index d98b293ed8daa..729ee0093b6dc 100644 --- a/pandas/tests/internals/test_external_block.py +++ b/pandas/tests/internals/test_external_block.py @@ -7,6 +7,8 @@ from pandas.core.internals import ( Block, BlockManager, SingleBlockManager, NonConsolidatableMixIn) +import pytest + class CustomBlock(NonConsolidatableMixIn, Block): @@ -25,6 +27,17 @@ def concat_same_type(self, to_concat, placement=None): values, placement=placement or slice(0, len(values), 1)) +@pytest.fixture +def df(): + df1 = pd.DataFrame({'a': [1, 2, 3]}) + blocks = df1._data.blocks + values = np.arange(3, dtype='int64') + custom_block = CustomBlock(values, placement=slice(1, 2)) + blocks = blocks + (custom_block,) + block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df1.index]) + return pd.DataFrame(block_manager) + + def test_custom_repr(): values = np.arange(3, dtype='int64') @@ -51,14 +64,14 @@ def test_concat_series(): assert isinstance(res._data.blocks[0], CustomBlock) -def test_concat_dataframe(): +def test_concat_dataframe(df): # GH17728 - df = pd.DataFrame({'a': [1, 2, 3]}) - blocks = df._data.blocks - values = np.arange(3, dtype='int64') - custom_block = CustomBlock(values, placement=slice(1, 2)) - blocks = blocks + (custom_block, ) - block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df.index]) - df = pd.DataFrame(block_manager) res = pd.concat([df, df]) assert isinstance(res._data.blocks[1], CustomBlock) + + +def test_concat_axis1(df): + # GH17954 + df2 = pd.DataFrame({'c': [.1, .2, .3]}) + res = pd.concat([df, df2], axis=1) + assert isinstance(res._data.blocks[1], CustomBlock)