@@ -1203,6 +1203,34 @@ def test_validation(self):
1203
1203
)
1204
1204
tm .assert_frame_equal (result , expected_3 )
1205
1205
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
+
1206
1234
# Dups on right
1207
1235
right_w_dups = concat ([right , DataFrame ({"a" : ["e" ], "c" : ["moo" ]}, index = [4 ])])
1208
1236
merge (
@@ -1213,6 +1241,14 @@ def test_validation(self):
1213
1241
validate = "one_to_many" ,
1214
1242
)
1215
1243
1244
+ merge (
1245
+ left ,
1246
+ right_w_dups ,
1247
+ left_index = True ,
1248
+ right_index = True ,
1249
+ validate = "left_total" ,
1250
+ )
1251
+
1216
1252
msg = "Merge keys are not unique in right dataset; not a one-to-one merge"
1217
1253
with pytest .raises (MergeError , match = msg ):
1218
1254
merge (
@@ -1237,6 +1273,13 @@ def test_validation(self):
1237
1273
right_index = True ,
1238
1274
validate = "many_to_one" ,
1239
1275
)
1276
+ merge (
1277
+ left_w_dups ,
1278
+ right ,
1279
+ left_index = True ,
1280
+ right_index = True ,
1281
+ validate = "left_total" ,
1282
+ )
1240
1283
1241
1284
msg = "Merge keys are not unique in left dataset; not a one-to-one merge"
1242
1285
with pytest .raises (MergeError , match = msg ):
@@ -1279,7 +1322,10 @@ def test_validation(self):
1279
1322
'- "one_to_one"\n '
1280
1323
'- "one_to_many"\n '
1281
1324
'- "many_to_one"\n '
1282
- '- "many_to_many"'
1325
+ '- "many_to_many"\n '
1326
+ '- "total"\n '
1327
+ '- "left_total"\n '
1328
+ '- "right_total"'
1283
1329
)
1284
1330
with pytest .raises (ValueError , match = msg ):
1285
1331
merge (left , right , on = "a" , validate = "jibberish" )
@@ -1323,6 +1369,33 @@ def test_validation(self):
1323
1369
result = merge (left , right , on = ["a" , "b" ], validate = "1:1" )
1324
1370
tm .assert_frame_equal (result , expected_multi )
1325
1371
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
+
1326
1399
def test_merge_two_empty_df_no_division_error (self ):
1327
1400
# GH17776, PR #17846
1328
1401
a = DataFrame ({"a" : [], "b" : [], "c" : []})
0 commit comments