@@ -1410,8 +1410,8 @@ def add_prefix(self, prefix):
1410
1410
-------
1411
1411
LongPanel
1412
1412
"""
1413
- f = (( '%s' % prefix ) + '%s' ). __mod__
1414
- return self .rename ( columns = f )
1413
+ new_data = self . _data . add_prefix ( prefix )
1414
+ return self ._constructor ( new_data )
1415
1415
1416
1416
def add_suffix (self , suffix ):
1417
1417
"""
@@ -1425,8 +1425,8 @@ def add_suffix(self, suffix):
1425
1425
-------
1426
1426
with_suffix : DataFrame
1427
1427
"""
1428
- f = ( '%s' + ( '%s' % suffix )). __mod__
1429
- return self .rename ( columns = f )
1428
+ new_data = self . _data . add_suffix ( suffix )
1429
+ return self ._constructor ( new_data )
1430
1430
1431
1431
#----------------------------------------------------------------------
1432
1432
# Arithmetic / combination related
@@ -2065,7 +2065,7 @@ def append(self, other):
2065
2065
return self ._constructor (data = new_data , index = new_index ,
2066
2066
columns = new_columns )
2067
2067
2068
- def join (self , other , on = None , how = None ):
2068
+ def join (self , other , on = None , how = None , lsuffix = '' , rsuffix = '' ):
2069
2069
"""
2070
2070
Join columns with other DataFrame either on index or on a key
2071
2071
column
@@ -2083,6 +2083,10 @@ def join(self, other, on=None, how=None):
2083
2083
* right: use input frame's index
2084
2084
* outer: form union of indexes
2085
2085
* inner: use intersection of indexes
2086
+ lsuffix : string
2087
+ Suffix to use from left frame's overlapping columns
2088
+ rsuffix : string
2089
+ Suffix to use from right frame's overlapping columns
2086
2090
2087
2091
Returns
2088
2092
-------
@@ -2092,29 +2096,32 @@ def join(self, other, on=None, how=None):
2092
2096
if how is not None :
2093
2097
raise Exception ('how parameter is not valid when '
2094
2098
'*on* specified' )
2095
- return self ._join_on (other , on )
2099
+ return self ._join_on (other , on , lsuffix , rsuffix )
2096
2100
else :
2097
2101
if how is None :
2098
2102
how = 'left'
2099
- return self ._join_index (other , how )
2103
+ return self ._join_index (other , how , lsuffix , rsuffix )
2100
2104
2101
- def _join_on (self , other , on ):
2105
+ def _join_on (self , other , on , lsuffix , rsuffix ):
2102
2106
if len (other .index ) == 0 :
2103
2107
return self
2104
2108
2105
2109
if on not in self :
2106
2110
raise Exception ('%s column not contained in this frame!' % on )
2107
2111
2112
+ this , other = self ._maybe_rename_join (other , lsuffix , rsuffix )
2108
2113
new_data = self ._data .join_on (other ._data , self [on ], axis = 1 )
2109
2114
return self ._constructor (new_data )
2110
2115
2111
- def _join_index (self , other , how ):
2116
+ def _join_index (self , other , how , lsuffix , rsuffix ):
2112
2117
join_index = self ._get_join_index (other , how )
2113
- this_data = self .reindex (join_index )._data
2114
- other_data = other .reindex (join_index )._data
2118
+
2119
+ this = self .reindex (join_index )
2120
+ other = other .reindex (join_index )
2121
+ this , other = this ._maybe_rename_join (other , lsuffix , rsuffix )
2115
2122
2116
2123
# merge blocks
2117
- merged_data = this_data . merge (other_data )
2124
+ merged_data = this . _data . merge (other . _data )
2118
2125
return self ._constructor (merged_data )
2119
2126
2120
2127
def _get_join_index (self , other , how ):
@@ -2131,6 +2138,30 @@ def _get_join_index(self, other, how):
2131
2138
2132
2139
return join_index
2133
2140
2141
+ def _maybe_rename_join (self , other , lsuffix , rsuffix ):
2142
+ intersection = self .columns .intersection (other .columns )
2143
+
2144
+ if len (intersection ) > 0 :
2145
+ if not lsuffix and not rsuffix :
2146
+ raise Exception ('columns overlap: %s' % intersection )
2147
+
2148
+ def lrenamer (x ):
2149
+ if x in intersection :
2150
+ return '%s%s' % (x , lsuffix )
2151
+ return x
2152
+
2153
+ def rrenamer (x ):
2154
+ if x in intersection :
2155
+ return '%s%s' % (x , rsuffix )
2156
+ return x
2157
+
2158
+ this = self .rename (columns = lrenamer )
2159
+ other = other .rename (columns = rrenamer )
2160
+ else :
2161
+ this = self
2162
+
2163
+ return this , other
2164
+
2134
2165
#----------------------------------------------------------------------
2135
2166
# Statistical methods, etc.
2136
2167
0 commit comments