Skip to content

Commit d5eead6

Browse files
swyoonjreback
authored andcommitted
DOC/ERR: better error message on no common merge keys (#19427)
1 parent 54f1b3e commit d5eead6

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ Reshaping
667667
- Bug in :func:`DataFrame.stack`, :func:`DataFrame.unstack`, :func:`Series.unstack` which were not returning subclasses (:issue:`15563`)
668668
- Bug in timezone comparisons, manifesting as a conversion of the index to UTC in ``.concat()`` (:issue:`18523`)
669669
- Bug in :func:`concat` when concatting sparse and dense series it returns only a ``SparseDataFrame``. Should be a ``DataFrame``. (:issue:`18914`, :issue:`18686`, and :issue:`16874`)
670+
- Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`)
670671
-
671672

672673

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
--------
234234
merge_ordered
235235
merge_asof
236-
236+
DataFrame.join
237237
"""
238238

239239
# -----------------------------------------------------------------------

pandas/core/reshape/merge.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,12 @@ def _validate_specification(self):
10211021
common_cols = self.left.columns.intersection(
10221022
self.right.columns)
10231023
if len(common_cols) == 0:
1024-
raise MergeError('No common columns to perform merge on')
1024+
raise MergeError(
1025+
'No common columns to perform merge on. '
1026+
'Merge options: left_on={lon}, right_on={ron}, '
1027+
'left_index={lidx}, right_index={ridx}'
1028+
.format(lon=self.left_on, ron=self.right_on,
1029+
lidx=self.left_index, ridx=self.right_index))
10251030
if not common_cols.is_unique:
10261031
raise MergeError("Data columns not unique: {common!r}"
10271032
.format(common=common_cols))

pandas/tests/reshape/merge/test_merge.py

+8
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ def test_no_overlap_more_informative_error(self):
270270
df2 = DataFrame({'y': ['b', 'c']}, index=[dt, dt])
271271
pytest.raises(MergeError, merge, df1, df2)
272272

273+
msg = ('No common columns to perform merge on. '
274+
'Merge options: left_on={lon}, right_on={ron}, '
275+
'left_index={lidx}, right_index={ridx}'
276+
.format(lon=None, ron=None, lidx=False, ridx=False))
277+
278+
with tm.assert_raises_regex(MergeError, msg):
279+
merge(df1, df2)
280+
273281
def test_merge_non_unique_indexes(self):
274282

275283
dt = datetime(2012, 5, 1)

0 commit comments

Comments
 (0)