Skip to content

CLN: Merge_asof null error message #23195

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 1 commit into from
Oct 17, 2018
Merged
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
6 changes: 3 additions & 3 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,18 +1390,18 @@ def flip(xs):
self.right_join_keys[-1])
tolerance = self.tolerance

# we required sortedness and non-missingness in the join keys
# we require sortedness and non-null values in the join keys
msg_sorted = "{side} keys must be sorted"
msg_missings = "Merge keys contain null values on {side} side"

if not Index(left_values).is_monotonic:
if isnull(left_values).sum() > 0:
if isnull(left_values).any():
raise ValueError(msg_missings.format(side='left'))
else:
raise ValueError(msg_sorted.format(side='left'))

if not Index(right_values).is_monotonic:
if isnull(right_values).sum() > 0:
if isnull(right_values).any():
raise ValueError(msg_missings.format(side='right'))
else:
raise ValueError(msg_sorted.format(side='right'))
Expand Down
33 changes: 14 additions & 19 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,24 +1008,19 @@ def test_merge_datatype_error(self):
with tm.assert_raises_regex(MergeError, msg):
merge_asof(left, right, on='a')

def test_merge_on_nans_int(self):
# 23189
msg = "Merge keys contain null values on left side"
left = pd.DataFrame({'a': [1.0, 5.0, 10.0, 12.0, np.nan],
'left_val': ['a', 'b', 'c', 'd', 'e']})
right = pd.DataFrame({'a': [1.0, 5.0, 10.0, 12.0],
'right_val': [1, 6, 11, 15]})

with tm.assert_raises_regex(ValueError, msg):
merge_asof(left, right, on='a')

def test_merge_on_nans_datetime(self):
# 23189
msg = "Merge keys contain null values on right side"
left = pd.DataFrame({"a": pd.date_range('20130101', periods=5)})
date_vals = pd.date_range('20130102', periods=5)\
.append(pd.Index([None]))
right = pd.DataFrame({"a": date_vals})
@pytest.mark.parametrize('func', [lambda x: x, lambda x: to_datetime(x)],
ids=['numeric', 'datetime'])
@pytest.mark.parametrize('side', ['left', 'right'])
def test_merge_on_nans(self, func, side):
# GH 23189
msg = "Merge keys contain null values on {} side".format(side)
nulls = func([1.0, 5.0, np.nan])
non_nulls = func([1.0, 5.0, 10.])
df_null = pd.DataFrame({'a': nulls, 'left_val': ['a', 'b', 'c']})
df = pd.DataFrame({'a': non_nulls, 'right_val': [1, 6, 11]})

with tm.assert_raises_regex(ValueError, msg):
merge_asof(left, right, on='a')
if side == 'left':
merge_asof(df_null, df, on='a')
else:
merge_asof(df, df_null, on='a')