Skip to content

Commit 5641665

Browse files
danieljreback
daniel
authored andcommitted
ENH: add error message for merge with non-DataFrame
closes #12081 closes #12112
1 parent 5de6b84 commit 5641665

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v0.18.0.txt

+2
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

+9
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ 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 '
190+
'type {0}'.format(type(left)))
191+
if not isinstance(right, DataFrame):
192+
raise ValueError(
193+
'can not merge DataFrame with instance of '
194+
'type {0}'.format(type(right)))
195+
187196
# note this function has side effects
188197
(self.left_join_keys,
189198
self.right_join_keys,

pandas/tools/tests/test_merge.py

+11
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ 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+
def test_join_on_fails_with_wrong_object_type(self):
265+
# GH12081
266+
wrongly_typed = [Series([0, 1]), 2, 'str', None, np.array([0, 1])]
267+
df = DataFrame({'a': [1, 1]})
268+
269+
for obj in wrongly_typed:
270+
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
271+
merge(obj, df, left_on='a', right_on='a')
272+
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
273+
merge(df, obj, left_on='a', right_on='a')
274+
264275
def test_join_on_pass_vector(self):
265276
expected = self.target.join(self.source, on='C')
266277
del expected['C']

0 commit comments

Comments
 (0)