Skip to content

Commit 007efb1

Browse files
REF series concat: concat blocks if all of same type
1 parent e2a0251 commit 007efb1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pandas/core/internals.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ def ftype(self):
312312
def merge(self, other):
313313
return _merge_blocks([self, other])
314314

315+
def concat_same_type(self, others):
316+
"""
317+
Concatenate list of single blocks of the same type.
318+
"""
319+
values = np.concatenate([self.values] + [o.values for o in others])
320+
return self.make_block_same_class(
321+
values, placement=slice(0, len(values), 1))
322+
315323
def reindex_axis(self, indexer, method=None, axis=1, fill_value=None,
316324
limit=None, mask_info=None):
317325
"""
@@ -2684,6 +2692,21 @@ def shift(self, periods, axis=0, mgr=None):
26842692
return [self.make_block_same_class(new_values,
26852693
placement=self.mgr_locs)]
26862694

2695+
def concat_same_type(self, others):
2696+
"""
2697+
Concatenate list of single blocks of the same type.
2698+
"""
2699+
# can maybe replace
2700+
# from pandas.core.dtypes.concat._concat_datetimetz ?
2701+
to_concat = [self.values] + [o.values for o in others]
2702+
2703+
if len(set([str(x.dtype) for x in to_concat])) != 1:
2704+
raise ValueError('to_concat must have the same tz')
2705+
2706+
values = to_concat[0]._concat_same_dtype(to_concat, None)
2707+
return self.make_block_same_class(
2708+
values, placement=slice(0, len(values), 1))
2709+
26872710

26882711
class SparseBlock(NonConsolidatableMixIn, Block):
26892712
""" implement as a list of sparse arrays of the same dtype """

pandas/core/reshape/concat.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,16 @@ def get_result(self):
362362

363363
# stack blocks
364364
if self.axis == 0:
365+
name = com._consensus_name_attr(self.objs)
366+
367+
# check if all series are of the same block type:
368+
blocks = [obj._data.blocks[0] for obj in self.objs]
369+
if all([type(b) == type(blocks[0]) for b in blocks[1:]]):
370+
new_block = blocks[0].concat_same_type(blocks[1:])
371+
return (Series(new_block, index=self.new_axes[0],
372+
name=name, fastpath=True)
373+
.__finalize__(self, method='concat'))
374+
365375
# concat Series with length to keep dtype as much
366376
non_empties = [x for x in self.objs if len(x) > 0]
367377
if len(non_empties) > 0:
@@ -370,7 +380,6 @@ def get_result(self):
370380
values = [x._values for x in self.objs]
371381
new_data = _concat._concat_compat(values)
372382

373-
name = com._consensus_name_attr(self.objs)
374383
cons = _concat._get_series_result_type(new_data)
375384

376385
return (cons(new_data, index=self.new_axes[0],

0 commit comments

Comments
 (0)