Skip to content

Commit

Permalink
BUG: close #871, concat on axis=1 and ignore_index=True fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
adamklein committed Mar 6, 2012
1 parent 1723257 commit a182a80
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
29 changes: 20 additions & 9 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,17 @@ def get_result(self):
new_data = self._get_concatenated_data()
return self.objs[0]._from_axes(new_data, self.new_axes)

def _get_fresh_axis(self):
return Index(np.arange(len(self._get_concat_axis())))

def _get_concatenated_data(self):
try:
# need to conform to same other (joined) axes for block join
reindexed_data = self._get_reindexed_data()

blockmaps = []
for data in reindexed_data:
data = data.consolidate()
type_map = dict((type(blk), blk) for blk in data.blocks)
blockmaps.append(type_map)
kinds = _get_all_block_kinds(blockmaps)
Expand All @@ -777,6 +781,10 @@ def _get_concatenated_data(self):
klass_blocks = [mapping.get(kind) for mapping in blockmaps]
stacked_block = self._concat_blocks(klass_blocks)
new_blocks.append(stacked_block)

if self.axis == 0 and self.ignore_index:
self.new_axes[0] = self._get_fresh_axis()

new_data = BlockManager(new_blocks, self.new_axes)
except Exception: # EAFP
# should not be possible to fail here for the expected reason with
Expand Down Expand Up @@ -820,16 +828,19 @@ def _concat_blocks(self, blocks):
'DataFrames')
return make_block(concat_values, blocks[0].items, self.new_axes[0])
else:
all_items = [b.items for b in blocks if b is not None]
if self.axis == 0 and self.keys is not None:
offsets = np.r_[0, np.cumsum([len(x._data.axes[self.axis]) for
x in self.objs])]
indexer = np.concatenate([offsets[i] + b.ref_locs
for i, b in enumerate(blocks)
if b is not None])
concat_items = self.new_axes[0].take(indexer)
offsets = np.r_[0, np.cumsum([len(x._data.axes[0]) for
x in self.objs])]
indexer = np.concatenate([offsets[i] + b.ref_locs
for i, b in enumerate(blocks)
if b is not None])
if self.ignore_index:
concat_items = indexer
else:
concat_items = _concat_indexes(all_items)
concat_items = self.new_axes[0].take(indexer)

if self.ignore_index:
ref_items = self._get_fresh_axis()
return make_block(concat_values, concat_items, ref_items)

return make_block(concat_values, concat_items, self.new_axes[0])

Expand Down
19 changes: 19 additions & 0 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,25 @@ def test_concat_dict(self):
expected = concat([frames[k] for k in keys], keys=keys)
tm.assert_frame_equal(result, expected)

def test_concat_ignore_index(self):
frame1 = DataFrame({"test1": ["a", "b", "c"],
"test2": [1,2,3],
"test3": [4.5, 3.2, 1.2]})
frame2 = DataFrame({"test3": [5.2, 2.2, 4.3]})
frame1.index = Index(["x", "y", "z"])
frame2.index = Index(["x", "y", "q"])

v1 = concat([frame1, frame2], axis=1, ignore_index=True)

nan = np.nan
expected = DataFrame([[nan,nan,nan, 4.3],
['a', 1, 4.5, 5.2],
['b', 2, 3.2, 2.2],
['c', 3, 1.2, nan]],
index=Index(["q", "x", "y", "z"]))

tm.assert_frame_equal(v1, expected)

def test_concat_multiindex_with_keys(self):
index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
['one', 'two', 'three']],
Expand Down

0 comments on commit a182a80

Please sign in to comment.