Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3702,6 +3702,7 @@ def append(self, other, ignore_index=False, verify_integrity=False):
verify_integrity=verify_integrity)

def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
lprefix='', rprefix='',
sort=False):
"""
Join columns with other DataFrame either on index or on a key
Expand Down Expand Up @@ -3746,9 +3747,11 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
"""
# For SparseDataFrame's benefit
return self._join_compat(other, on=on, how=how, lsuffix=lsuffix,
rsuffix=rsuffix, sort=sort)
rsuffix=rsuffix, lprefix=lprefix,
rprefix=rprefix, sort=sort)

def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
lprefix='', rprefix='',
sort=False):
from pandas.tools.merge import merge, concat

Expand All @@ -3760,7 +3763,9 @@ def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
if isinstance(other, DataFrame):
return merge(self, other, left_on=on, how=how,
left_index=on is None, right_index=True,
suffixes=(lsuffix, rsuffix), sort=sort)
suffixes=(lsuffix, rsuffix),
prefixes=(lprefix, rprefix),
sort=sort)
else:
if on is not None:
raise ValueError('Joining multiple DataFrames only supported'
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3898,6 +3898,31 @@ def rrenamer(x):
_transform_index(right, rrenamer))


def items_overlap_with_suffix_and_prefix(
left, lsuffix, lprefix, right, rsuffix, rprefix):
to_rename = left.intersection(right)
if len(to_rename) == 0:
return left, right
else:
if not lsuffix and not rsuffix and not lprefix and not rprefix:
raise ValueError('columns overlap but no suffix or prefix specified: %s' %
to_rename)

def lrenamer(x):
if x in to_rename:
return '%s%s%s' % (lprefix, x, lsuffix)
return x

def rrenamer(x):
if x in to_rename:
return '%s%s%s' % (rprefix, x, rsuffix)
return x

return (_transform_index(left, lrenamer),
_transform_index(right, rrenamer))



def _transform_index(index, func):
"""
Apply function to all values found in index.
Expand Down
18 changes: 9 additions & 9 deletions pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,13 @@ def _reindex_with_indexers(self, reindexers, method=None, fill_value=None, limit
return SparseDataFrame(new_arrays, index=index, columns=columns).__finalize__(self)

def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
sort=False):
lprefix='', rprefix='', sort=False):
if on is not None:
raise NotImplementedError("'on' keyword parameter is not yet "
"implemented")
return self._join_index(other, how, lsuffix, rsuffix)
return self._join_index(other, how, lsuffix, rsuffix, lprefix, rprefix)

def _join_index(self, other, how, lsuffix, rsuffix):
def _join_index(self, other, how, lsuffix, rsuffix, lprefix, rprefix):
if isinstance(other, Series):
if other.name is None:
raise ValueError('Other Series must have a name')
Expand All @@ -626,26 +626,26 @@ def _join_index(self, other, how, lsuffix, rsuffix):
this = self.reindex(join_index)
other = other.reindex(join_index)

this, other = this._maybe_rename_join(other, lsuffix, rsuffix)
this, other = this._maybe_rename_join(other, lsuffix, rsuffix, lprefix, rprefix)

from pandas import concat
return concat([this, other], axis=1, verify_integrity=True)

def _maybe_rename_join(self, other, lsuffix, rsuffix):
def _maybe_rename_join(self, other, lsuffix, rsuffix, lprefix, rprefix):
to_rename = self.columns.intersection(other.columns)
if len(to_rename) > 0:
if not lsuffix and not rsuffix:
raise ValueError('columns overlap but no suffix specified: %s'
if not lsuffix and not rsuffix and not lprefix and not rprefix:
raise ValueError('columns overlap but no suffix or prefix specified: %s'
% to_rename)

def lrenamer(x):
if x in to_rename:
return '%s%s' % (x, lsuffix)
return '%s%s%s' % (lprefix, x, lsuffix)
return x

def rrenamer(x):
if x in to_rename:
return '%s%s' % (x, rsuffix)
return '%s%s%s' % (rprefix, x, rsuffix)
return x

this = self.rename(columns=lrenamer)
Expand Down
14 changes: 10 additions & 4 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
_ensure_index, _get_consensus_names,
_all_indexes_same)
from pandas.core.internals import (items_overlap_with_suffix,
items_overlap_with_suffix_and_prefix,
concatenate_block_managers)
from pandas.util.decorators import Appender, Substitution
from pandas.core.common import ABCSeries
Expand All @@ -31,10 +32,11 @@
@Appender(_merge_doc, indents=0)
def merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=False,
suffixes=('_x', '_y'), copy=True):
suffixes=('_x', '_y'), prefixes=('', ''), copy=True):
op = _MergeOperation(left, right, how=how, on=on, left_on=left_on,
right_on=right_on, left_index=left_index,
right_index=right_index, sort=sort, suffixes=suffixes,
prefixes=prefixes,
copy=copy)
return op.get_result()
if __debug__:
Expand Down Expand Up @@ -161,7 +163,7 @@ class _MergeOperation(object):
def __init__(self, left, right, how='inner', on=None,
left_on=None, right_on=None, axis=1,
left_index=False, right_index=False, sort=True,
suffixes=('_x', '_y'), copy=True):
suffixes=('_x', '_y'), prefixes=('', ''), copy=True):
self.left = self.orig_left = left
self.right = self.orig_right = right
self.how = how
Expand All @@ -173,6 +175,7 @@ def __init__(self, left, right, how='inner', on=None,

self.copy = copy
self.suffixes = suffixes
self.prefixes = prefixes
self.sort = sort

self.left_index = left_index
Expand All @@ -188,9 +191,12 @@ def get_result(self):

ldata, rdata = self.left._data, self.right._data
lsuf, rsuf = self.suffixes
lpre, rpre = self.prefixes

llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf,
rdata.items, rsuf)
llabels, rlabels = items_overlap_with_suffix_and_prefix(
ldata.items, lsuf, lpre,
rdata.items, rsuf, rpre
)

lindexers = {1: left_indexer} if left_indexer is not None else {}
rindexers = {1: right_indexer} if right_indexer is not None else {}
Expand Down