Skip to content

ENH: add error message for merge with Series GH12081 #12112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ In addition, ``.round()``, ``.floor()`` and ``.ceil()`` will be available thru t

.. _whatsnew_0180.api:

- ``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`)

.. _whatsnew_0180.api_breaking:

Backwards incompatible API changes
Expand Down
7 changes: 7 additions & 0 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def __init__(self, left, right, how='inner', on=None,
raise ValueError(
'indicator option can only accept boolean or string arguments')

if not isinstance(left, DataFrame):
raise ValueError(
'can not merge DataFrame with instance of type {0}'.format(type(left)))
if not isinstance(right, DataFrame):
raise ValueError(
'can not merge DataFrame with instance of type {0}'.format(type(right)))

# note this function has side effects
(self.left_join_keys,
self.right_join_keys,
Expand Down
12 changes: 12 additions & 0 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ def test_join_on_fails_with_different_column_counts(self):
index=tm.makeCustomIndex(10, 2))
merge(df, df2, right_on='a', left_on=['a', 'b'])


def test_join_on_fails_with_wrong_object_type(self):
# GH12081
wrongly_typed = [Series([0, 1]), 2, 'str', None, np.ndarray([0, 1])]
df = DataFrame({'a': [1, 1]})

for obj in wrongly_typed:
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
merge(obj, df, left_on='a', right_on='a')
with tm.assertRaisesRegexp(ValueError, str(type(obj))):
merge(df, obj, left_on='a', right_on='a')

def test_join_on_pass_vector(self):
expected = self.target.join(self.source, on='C')
del expected['C']
Expand Down