|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import copy
|
| 6 | +from functools import partial |
6 | 7 | import string
|
7 | 8 | import warnings
|
8 | 9 |
|
|
27 | 28 | from pandas.core.arrays.categorical import _recode_for_categories
|
28 | 29 | import pandas.core.common as com
|
29 | 30 | from pandas.core.frame import _merge_doc
|
30 |
| -from pandas.core.internals import ( |
31 |
| - concatenate_block_managers, items_overlap_with_suffix) |
| 31 | +from pandas.core.internals import _transform_index, concatenate_block_managers |
32 | 32 | import pandas.core.sorting as sorting
|
33 | 33 | from pandas.core.sorting import is_int64_overflow_possible
|
34 | 34 |
|
@@ -555,8 +555,8 @@ def get_result(self):
|
555 | 555 | ldata, rdata = self.left._data, self.right._data
|
556 | 556 | lsuf, rsuf = self.suffixes
|
557 | 557 |
|
558 |
| - llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf, |
559 |
| - rdata.items, rsuf) |
| 558 | + llabels, rlabels = _items_overlap_with_suffix(ldata.items, lsuf, |
| 559 | + rdata.items, rsuf) |
560 | 560 |
|
561 | 561 | lindexers = {1: left_indexer} if left_indexer is not None else {}
|
562 | 562 | rindexers = {1: right_indexer} if right_indexer is not None else {}
|
@@ -1303,8 +1303,8 @@ def get_result(self):
|
1303 | 1303 | ldata, rdata = self.left._data, self.right._data
|
1304 | 1304 | lsuf, rsuf = self.suffixes
|
1305 | 1305 |
|
1306 |
| - llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf, |
1307 |
| - rdata.items, rsuf) |
| 1306 | + llabels, rlabels = _items_overlap_with_suffix(ldata.items, lsuf, |
| 1307 | + rdata.items, rsuf) |
1308 | 1308 |
|
1309 | 1309 | if self.fill_method == 'ffill':
|
1310 | 1310 | left_join_indexer = libjoin.ffill_indexer(left_indexer)
|
@@ -1809,3 +1809,45 @@ def validate_operand(obj):
|
1809 | 1809 | else:
|
1810 | 1810 | raise TypeError('Can only merge Series or DataFrame objects, '
|
1811 | 1811 | 'a {obj} was passed'.format(obj=type(obj)))
|
| 1812 | + |
| 1813 | + |
| 1814 | +def _items_overlap_with_suffix(left, lsuffix, right, rsuffix): |
| 1815 | + """ |
| 1816 | + If two indices overlap, add suffixes to overlapping entries. |
| 1817 | +
|
| 1818 | + If corresponding suffix is empty, the entry is simply converted to string. |
| 1819 | +
|
| 1820 | + """ |
| 1821 | + to_rename = left.intersection(right) |
| 1822 | + if len(to_rename) == 0: |
| 1823 | + return left, right |
| 1824 | + |
| 1825 | + if not lsuffix and not rsuffix: |
| 1826 | + raise ValueError('columns overlap but no suffix specified: ' |
| 1827 | + '{rename}'.format(rename=to_rename)) |
| 1828 | + |
| 1829 | + def renamer(x, suffix): |
| 1830 | + """ |
| 1831 | + Rename the left and right indices. |
| 1832 | +
|
| 1833 | + If there is overlap, and suffix is not None, add |
| 1834 | + suffix, otherwise, leave it as-is. |
| 1835 | +
|
| 1836 | + Parameters |
| 1837 | + ---------- |
| 1838 | + x : original column name |
| 1839 | + suffix : str or None |
| 1840 | +
|
| 1841 | + Returns |
| 1842 | + ------- |
| 1843 | + x : renamed column name |
| 1844 | + """ |
| 1845 | + if x in to_rename and suffix is not None: |
| 1846 | + return '{x}{suffix}'.format(x=x, suffix=suffix) |
| 1847 | + return x |
| 1848 | + |
| 1849 | + lrenamer = partial(renamer, suffix=lsuffix) |
| 1850 | + rrenamer = partial(renamer, suffix=rsuffix) |
| 1851 | + |
| 1852 | + return (_transform_index(left, lrenamer), |
| 1853 | + _transform_index(right, rrenamer)) |
0 commit comments