Skip to content

change merge validate errors to MergeError from ValueError #16436

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Backwards incompatible API changes
Other API Changes
^^^^^^^^^^^^^^^^^

- Moved definition of ``MergeError`` to ``pandas/errors/__init__.py``.


.. _whatsnew_0210.deprecations:
Expand Down
15 changes: 6 additions & 9 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import pandas.core.algorithms as algos
import pandas.core.common as com
from pandas._libs import hashtable as libhashtable, join as libjoin, lib
from pandas.errors import MergeError


@Substitution('\nleft : DataFrame')
Expand All @@ -60,10 +61,6 @@ def merge(left, right, how='inner', on=None, left_on=None, right_on=None,
merge.__doc__ = _merge_doc % '\nleft : DataFrame'


class MergeError(ValueError):
pass


def _groupby_and_merge(by, on, left, right, _merge_pieces,
check_duplicates=True):
"""
Expand Down Expand Up @@ -986,23 +983,23 @@ def _validate(self, validate):
# Check data integrity
if validate in ["one_to_one", "1:1"]:
if not left_unique and not right_unique:
raise ValueError("Merge keys are not unique in either left"
raise MergeError("Merge keys are not unique in either left"
" or right dataset; not a one-to-one merge")
elif not left_unique:
raise ValueError("Merge keys are not unique in left dataset;"
raise MergeError("Merge keys are not unique in left dataset;"
" not a one-to-one merge")
elif not right_unique:
raise ValueError("Merge keys are not unique in right dataset;"
raise MergeError("Merge keys are not unique in right dataset;"
" not a one-to-one merge")

elif validate in ["one_to_many", "1:m"]:
if not left_unique:
raise ValueError("Merge keys are not unique in left dataset;"
raise MergeError("Merge keys are not unique in left dataset;"
"not a one-to-many merge")

elif validate in ["many_to_one", "m:1"]:
if not right_unique:
raise ValueError("Merge keys are not unique in right dataset;"
raise MergeError("Merge keys are not unique in right dataset;"
" not a many-to-one merge")

elif validate in ['many_to_many', 'm:m']:
Expand Down
6 changes: 6 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ class ParserWarning(Warning):
"""


class MergeError(ValueError):
"""
Error raised when problems arise during merging due to problems
with input data. Subclass of `ValueError`.
"""
14 changes: 7 additions & 7 deletions pandas/tests/reshape/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,11 @@ def test_validation(self):
merge(left, right_w_dups, left_index=True, right_index=True,
validate='one_to_many')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left, right_w_dups, left_index=True, right_index=True,
validate='one_to_one')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left, right_w_dups, on='a', validate='one_to_one')

# Dups on left
Expand All @@ -802,21 +802,21 @@ def test_validation(self):
merge(left_w_dups, right, left_index=True, right_index=True,
validate='many_to_one')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left_w_dups, right, left_index=True, right_index=True,
validate='one_to_one')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left_w_dups, right, on='a', validate='one_to_one')

# Dups on both
merge(left_w_dups, right_w_dups, on='a', validate='many_to_many')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left_w_dups, right_w_dups, left_index=True,
right_index=True, validate='many_to_one')

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left_w_dups, right_w_dups, on='a',
validate='one_to_many')

Expand All @@ -842,7 +842,7 @@ def test_validation(self):
'um... weasel noise?']},
index=range(3))

with pytest.raises(ValueError):
with pytest.raises(MergeError):
merge(left, right, on='a', validate='1:1')

result = merge(left, right, on=['a', 'b'], validate='1:1')
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"exc", ['UnsupportedFunctionCall', 'UnsortedIndexError',
'OutOfBoundsDatetime',
'ParserError', 'PerformanceWarning', 'DtypeWarning',
'EmptyDataError', 'ParserWarning'])
'EmptyDataError', 'ParserWarning', 'MergeError'])
def test_exception_importable(exc):
from pandas import errors
e = getattr(errors, exc)
Expand Down