Skip to content

Commit aeabda1

Browse files
committed
Merge pull request #4410 from jreback/df_rename
BUG: Fix an issue in merging blocks where the resulting DataFrame had partially set _ref_locs
2 parents b89c88a + 36031da commit aeabda1

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

doc/source/release.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ pandas 0.13
9494
- Fixed an issue where ``PeriodIndex`` joining with self was returning a new
9595
instance rather than the same instance (:issue:`4379`); also adds a test
9696
for this for the other index types
97-
- Fixed a bug with all the dtypes being converted to object when using the CSV cparser
97+
- Fixed a bug with all the dtypes being converted to object when using the CSV cparser
9898
with the usecols parameter (:issue: `3192`)
99+
- Fix an issue in merging blocks where the resulting DataFrame had partially
100+
set _ref_locs (:issue:`4403`)
99101

100102
pandas 0.12
101103
===========

pandas/tests/test_frame.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
from __future__ import print_function
24
# pylint: disable-msg=W0612,E1101
35
from copy import deepcopy
@@ -2956,6 +2958,27 @@ def check(result, expected=None):
29562958
expected = np.array([[1,2.5],[3,4.5]])
29572959
self.assert_((result == expected).all().all())
29582960

2961+
# rename, GH 4403
2962+
df4 = DataFrame({'TClose': [22.02],
2963+
'RT': [0.0454],
2964+
'TExg': [0.0422]},
2965+
index=MultiIndex.from_tuples([(600809, 20130331)], names=['STK_ID', 'RPT_Date']))
2966+
2967+
df5 = DataFrame({'STK_ID': [600809] * 3,
2968+
'RPT_Date': [20120930,20121231,20130331],
2969+
'STK_Name': [u('饡驦'), u('饡驦'), u('饡驦')],
2970+
'TClose': [38.05, 41.66, 30.01]},
2971+
index=MultiIndex.from_tuples([(600809, 20120930), (600809, 20121231),(600809,20130331)], names=['STK_ID', 'RPT_Date']))
2972+
2973+
k = pd.merge(df4,df5,how='inner',left_index=True,right_index=True)
2974+
result = k.rename(columns={'TClose_x':'TClose', 'TClose_y':'QT_Close'})
2975+
str(result)
2976+
result.dtypes
2977+
2978+
expected = DataFrame([[0.0454, 22.02, 0.0422, 20130331, 600809, u('饡驦'), 30.01 ]],
2979+
columns=['RT','TClose','TExg','RPT_Date','STK_ID','STK_Name','QT_Close']).set_index(['STK_ID','RPT_Date'],drop=False)
2980+
assert_frame_equal(result,expected)
2981+
29592982
def test_insert_benchmark(self):
29602983
# from the vb_suite/frame_methods/frame_insert_columns
29612984
N = 10

pandas/tools/merge.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ def get_result(self):
683683
blockmaps = self._prepare_blocks()
684684
kinds = _get_merge_block_kinds(blockmaps)
685685

686+
result_is_unique = self.result_axes[0].is_unique
686687
result_blocks = []
687688

688689
# maybe want to enable flexible copying <-- what did I mean?
@@ -692,6 +693,12 @@ def get_result(self):
692693
if klass in mapping:
693694
klass_blocks.extend((unit, b) for b in mapping[klass])
694695
res_blk = self._get_merged_block(klass_blocks)
696+
697+
# if we have a unique result index, need to clear the _ref_locs
698+
# a non-unique is set as we are creating
699+
if result_is_unique:
700+
res_blk.set_ref_locs(None)
701+
695702
result_blocks.append(res_blk)
696703

697704
return BlockManager(result_blocks, self.result_axes)
@@ -1070,7 +1077,7 @@ def _concat_blocks(self, blocks):
10701077
# map the column location to the block location
10711078
# GH3602
10721079
if not self.new_axes[0].is_unique:
1073-
block._ref_locs = indexer
1080+
block.set_ref_locs(indexer)
10741081

10751082
return block
10761083

0 commit comments

Comments
 (0)