Skip to content

Commit 2a5abb0

Browse files
committed
BUG: don't assume that each object contains every unique block type in concat, GH #708
1 parent 00993bc commit 2a5abb0

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pandas/tools/merge.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,7 @@ def _get_concatenated_data(self):
730730

731731
new_blocks = []
732732
for kind in kinds:
733-
klass_blocks = [mapping.get(kind) for mapping in blockmaps
734-
if kind in mapping]
733+
klass_blocks = [mapping.get(kind) for mapping in blockmaps]
735734
stacked_block = self._concat_blocks(klass_blocks)
736735
new_blocks.append(stacked_block)
737736
new_data = BlockManager(new_blocks, self.new_axes)
@@ -766,7 +765,8 @@ def _get_reindexed_data(self):
766765
return reindexed_data
767766

768767
def _concat_blocks(self, blocks):
769-
concat_values = np.concatenate([b.values for b in blocks],
768+
concat_values = np.concatenate([b.values for b in blocks
769+
if b is not None],
770770
axis=self.axis)
771771

772772
if self.axis > 0:
@@ -776,12 +776,13 @@ def _concat_blocks(self, blocks):
776776
'DataFrames')
777777
return make_block(concat_values, blocks[0].items, self.new_axes[0])
778778
else:
779-
all_items = [b.items for b in blocks]
779+
all_items = [b.items for b in blocks if b is not None]
780780
if self.axis == 0 and self.keys is not None:
781781
offsets = np.r_[0, np.cumsum([len(x._data.axes[self.axis]) for
782782
x in self.objs])]
783783
indexer = np.concatenate([offsets[i] + b.ref_locs
784-
for i, b in enumerate(blocks)])
784+
for i, b in enumerate(blocks)
785+
if b is not None])
785786
concat_items = self.new_axes[0].take(indexer)
786787
else:
787788
concat_items = _concat_indexes(all_items)

pandas/tools/tests/test_merge.py

+11
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,17 @@ def test_concat_keys_specific_levels(self):
854854
self.assert_(np.array_equal(result.columns.levels[0], level))
855855
self.assertEqual(result.columns.names[0], 'group_key')
856856

857+
def test_concat_dataframe_keys_bug(self):
858+
t1 = DataFrame({'value': Series([1,2,3],
859+
index=Index(['a', 'b', 'c'], name='id'))})
860+
t2 = DataFrame({'value': Series([7, 8],
861+
index=Index(['a', 'b'], name = 'id'))})
862+
863+
# it works
864+
result = concat([t1, t2], axis=1, keys=['t1', 't2'])
865+
self.assertEqual(list(result.columns), [('t1', 'value'),
866+
('t2', 'value')])
867+
857868
def test_concat_dict(self):
858869
frames = {'foo' : DataFrame(np.random.randn(4, 3)),
859870
'bar' : DataFrame(np.random.randn(4, 3)),

0 commit comments

Comments
 (0)