Skip to content

Commit 0189ab8

Browse files
committed
Add tests for totality validation
1 parent fe63b4f commit 0189ab8

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

pandas/tests/reshape/merge/test_merge.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,34 @@ def test_validation(self):
12031203
)
12041204
tm.assert_frame_equal(result, expected_3)
12051205

1206+
# Make sure left totality works
1207+
result = merge(
1208+
left, right, left_index=True, right_index=True, validate="one_to_one+left_total"
1209+
)
1210+
tm.assert_frame_equal(result, expected)
1211+
1212+
# Make sure right totality raises exception
1213+
msg = "Merge keys in right dataset are not all present in the left dataset; not a right total merge"
1214+
with pytest.raises(MergeError, match=msg):
1215+
merge(
1216+
left,
1217+
right,
1218+
left_index=True,
1219+
right_index=True,
1220+
validate="one_to_one+right_total",
1221+
)
1222+
1223+
# Make sure general totality raises exception
1224+
msg = "Merge keys in right dataset are not all present in the left dataset; not a total merge"
1225+
with pytest.raises(MergeError, match=msg):
1226+
merge(
1227+
left,
1228+
right,
1229+
left_index=True,
1230+
right_index=True,
1231+
validate="one_to_one+total",
1232+
)
1233+
12061234
# Dups on right
12071235
right_w_dups = concat([right, DataFrame({"a": ["e"], "c": ["moo"]}, index=[4])])
12081236
merge(
@@ -1213,6 +1241,14 @@ def test_validation(self):
12131241
validate="one_to_many",
12141242
)
12151243

1244+
merge(
1245+
left,
1246+
right_w_dups,
1247+
left_index=True,
1248+
right_index=True,
1249+
validate="left_total",
1250+
)
1251+
12161252
msg = "Merge keys are not unique in right dataset; not a one-to-one merge"
12171253
with pytest.raises(MergeError, match=msg):
12181254
merge(
@@ -1237,6 +1273,13 @@ def test_validation(self):
12371273
right_index=True,
12381274
validate="many_to_one",
12391275
)
1276+
merge(
1277+
left_w_dups,
1278+
right,
1279+
left_index=True,
1280+
right_index=True,
1281+
validate="left_total",
1282+
)
12401283

12411284
msg = "Merge keys are not unique in left dataset; not a one-to-one merge"
12421285
with pytest.raises(MergeError, match=msg):
@@ -1279,7 +1322,10 @@ def test_validation(self):
12791322
'- "one_to_one"\n'
12801323
'- "one_to_many"\n'
12811324
'- "many_to_one"\n'
1282-
'- "many_to_many"'
1325+
'- "many_to_many"\n'
1326+
'- "total"\n'
1327+
'- "left_total"\n'
1328+
'- "right_total"'
12831329
)
12841330
with pytest.raises(ValueError, match=msg):
12851331
merge(left, right, on="a", validate="jibberish")
@@ -1323,6 +1369,33 @@ def test_validation(self):
13231369
result = merge(left, right, on=["a", "b"], validate="1:1")
13241370
tm.assert_frame_equal(result, expected_multi)
13251371

1372+
right_total_ext = concat(
1373+
[right, DataFrame({"a": ["b"], "b": [1], "d": ["neigh"]}, index=[3])], sort=True
1374+
)
1375+
expected_total_ext = DataFrame(
1376+
{
1377+
"a": ["a", "a", "b", "b"],
1378+
"b": [0, 1, 0, 1],
1379+
"c": ["cat", "dog", "weasel", "horse"],
1380+
"d": ["meow", "bark", "um... weasel noise?", "neigh"],
1381+
},
1382+
index=range(4),
1383+
)
1384+
result = merge(left, right_total_ext, on=["a", "b"], validate="1:1+total")
1385+
tm.assert_frame_equal(result, expected_total_ext)
1386+
1387+
# Ensure not left total raises error
1388+
right_reduced = right.drop_duplicates(subset=['b'])
1389+
1390+
msg = "Merge keys in left dataset are not all present in the right dataset; not a left total merge"
1391+
with pytest.raises(MergeError, match=msg):
1392+
merge(
1393+
left,
1394+
right_reduced,
1395+
on=["a", "b"],
1396+
validate="left_total",
1397+
)
1398+
13261399
def test_merge_two_empty_df_no_division_error(self):
13271400
# GH17776, PR #17846
13281401
a = DataFrame({"a": [], "b": [], "c": []})

0 commit comments

Comments
 (0)