Skip to content

Commit e38bd74

Browse files
jorisvandenbosscheTomAugspurger
authored andcommitted
REF/INT: preserve block type in joining when only having a single block (#17954)
* REF/INT: preserve block type in joining when only having a single block * add test * remove checking of values.base * clean-up comment
1 parent 79498e3 commit e38bd74

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

pandas/core/internals.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5182,7 +5182,15 @@ def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy):
51825182

51835183
for placement, join_units in concat_plan:
51845184

5185-
if is_uniform_join_units(join_units):
5185+
if len(join_units) == 1 and not join_units[0].indexers:
5186+
b = join_units[0].block
5187+
values = b.values
5188+
if copy:
5189+
values = values.copy()
5190+
elif not copy:
5191+
values = values.view()
5192+
b = b.make_block_same_class(values, placement=placement)
5193+
elif is_uniform_join_units(join_units):
51865194
b = join_units[0].block.concat_same_type(
51875195
[ju.block for ju in join_units], placement=placement)
51885196
else:

pandas/tests/internals/test_external_block.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pandas.core.internals import (
88
Block, BlockManager, SingleBlockManager, NonConsolidatableMixIn)
99

10+
import pytest
11+
1012

1113
class CustomBlock(NonConsolidatableMixIn, Block):
1214

@@ -25,6 +27,17 @@ def concat_same_type(self, to_concat, placement=None):
2527
values, placement=placement or slice(0, len(values), 1))
2628

2729

30+
@pytest.fixture
31+
def df():
32+
df1 = pd.DataFrame({'a': [1, 2, 3]})
33+
blocks = df1._data.blocks
34+
values = np.arange(3, dtype='int64')
35+
custom_block = CustomBlock(values, placement=slice(1, 2))
36+
blocks = blocks + (custom_block,)
37+
block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df1.index])
38+
return pd.DataFrame(block_manager)
39+
40+
2841
def test_custom_repr():
2942
values = np.arange(3, dtype='int64')
3043

@@ -51,14 +64,14 @@ def test_concat_series():
5164
assert isinstance(res._data.blocks[0], CustomBlock)
5265

5366

54-
def test_concat_dataframe():
67+
def test_concat_dataframe(df):
5568
# GH17728
56-
df = pd.DataFrame({'a': [1, 2, 3]})
57-
blocks = df._data.blocks
58-
values = np.arange(3, dtype='int64')
59-
custom_block = CustomBlock(values, placement=slice(1, 2))
60-
blocks = blocks + (custom_block, )
61-
block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df.index])
62-
df = pd.DataFrame(block_manager)
6369
res = pd.concat([df, df])
6470
assert isinstance(res._data.blocks[1], CustomBlock)
71+
72+
73+
def test_concat_axis1(df):
74+
# GH17954
75+
df2 = pd.DataFrame({'c': [.1, .2, .3]})
76+
res = pd.concat([df, df2], axis=1)
77+
assert isinstance(res._data.blocks[1], CustomBlock)

0 commit comments

Comments
 (0)