Skip to content

Commit 7676f03

Browse files
move logic into SingleBlockManager.concat
1 parent d4ce3df commit 7676f03

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

pandas/core/internals.py

+24
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,30 @@ def fast_xs(self, loc):
45344534
"""
45354535
return self._block.values[loc]
45364536

4537+
def concat(self, to_concat, new_axis):
4538+
4539+
non_empties = [x for x in to_concat if len(x) > 0]
4540+
4541+
# check if all series are of the same block type:
4542+
if len(non_empties) > 0:
4543+
blocks = [obj.blocks[0] for obj in non_empties]
4544+
4545+
if all([type(b) is type(blocks[0]) for b in blocks[1:]]): # noqa
4546+
new_block = blocks[0].concat_same_type(blocks)
4547+
else:
4548+
values = [x.values for x in blocks]
4549+
values = _concat._concat_compat(values)
4550+
new_block = make_block(
4551+
values, placement=slice(0, len(values), 1))
4552+
else:
4553+
values = [x._block.values for x in to_concat]
4554+
values = _concat._concat_compat(values)
4555+
new_block = make_block(
4556+
values, placement=slice(0, len(values), 1))
4557+
4558+
mgr = SingleBlockManager(new_block, new_axis)
4559+
return mgr
4560+
45374561

45384562
def construction_error(tot_items, block_shape, axes, e=None):
45394563
""" raise a helpful message about our construction """

pandas/core/reshape/concat.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -364,33 +364,15 @@ def get_result(self):
364364
if self.axis == 0:
365365
name = com._consensus_name_attr(self.objs)
366366

367-
# concat Series with length to keep dtype as much
368-
non_empties = [x for x in self.objs if len(x) > 0]
369-
370-
# check if all series are of the same block type:
371-
if len(non_empties) > 0:
372-
blocks = [obj._data.blocks[0] for obj in non_empties]
373-
if all([type(b) is type(blocks[0]) for b in blocks[1:]]): # noqa
374-
new_block = blocks[0].concat_same_type(blocks)
375-
if isinstance(new_block, SparseBlock):
376-
cons = SparseSeries
377-
else:
378-
cons = Series
379-
return (cons(new_block, index=self.new_axes[0],
380-
name=name, fastpath=True)
381-
.__finalize__(self, method='concat'))
382-
383-
if len(non_empties) > 0:
384-
values = [x._values for x in non_empties]
385-
else:
386-
values = [x._values for x in self.objs]
387-
new_data = _concat._concat_compat(values)
367+
mgr = self.objs[0]._data.concat([x._data for x in self.objs],
368+
self.new_axes)
388369

389-
cons = _concat._get_series_result_type(new_data)
370+
if mgr._block.is_sparse:
371+
cons = SparseSeries
372+
else:
373+
cons = self.objs[0].__class__
390374

391-
return (cons(new_data, index=self.new_axes[0],
392-
name=name, dtype=new_data.dtype)
393-
.__finalize__(self, method='concat'))
375+
return cons(mgr, name=name).__finalize__(self, method='concat')
394376

395377
# combine as columns in a frame
396378
else:

0 commit comments

Comments
 (0)