Skip to content

Commit 40ced26

Browse files
author
daniel
committed
ENH: add error message for merge with Series GH12081
1 parent 81dfb28 commit 40ced26

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ In addition, ``.round()``, ``.floor()`` and ``.ceil()`` will be available thru t
203203

204204
.. _whatsnew_0180.api:
205205

206+
- ``pandas.merge()`` and ``DataFrame.merge()`` will show a specific error message when trying to merge with an object that is not of type ``DataFrame`` or a subclass (:issue:`12081`)
207+
206208
.. _whatsnew_0180.api_breaking:
207209

208210
Backwards incompatible API changes

pandas/tools/merge.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ def __init__(self, left, right, how='inner', on=None,
184184
raise ValueError(
185185
'indicator option can only accept boolean or string arguments')
186186

187+
if not isinstance(left, DataFrame):
188+
raise ValueError(
189+
'can not merge DataFrame with instance of type {0}'.format(type(left)))
190+
if not isinstance(right, DataFrame):
191+
raise ValueError(
192+
'can not merge DataFrame with instance of type {0}'.format(type(right)))
193+
187194
# note this function has side effects
188195
(self.left_join_keys,
189196
self.right_join_keys,

pandas/tools/tests/test_merge.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ def test_join_on_fails_with_different_column_counts(self):
261261
index=tm.makeCustomIndex(10, 2))
262262
merge(df, df2, right_on='a', left_on=['a', 'b'])
263263

264+
265+
def test_join_on_fails_with_wrong_object_type(self):
266+
# GH12081
267+
wrongly_typed = [Series([0, 1]), 2, 'str', None, np.ndarray([0, 1])]
268+
df = DataFrame({'a': [1, 1]})
269+
270+
for obj in wrongly_typed:
271+
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
272+
merge(obj, df, left_on='a', right_on='a')
273+
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
274+
merge(df, obj, left_on='a', right_on='a')
275+
264276
def test_join_on_pass_vector(self):
265277
expected = self.target.join(self.source, on='C')
266278
del expected['C']

0 commit comments

Comments
 (0)