Skip to content

Commit 41b3571

Browse files
committed
Added test to check whether merge_asof correctly sets the allow duplicates flag.
1 parent d22b90a commit 41b3571

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

pandas/tests/generic/test_finalize.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -679,21 +679,58 @@ def test_finalize_frame_series_name():
679679
(True, False),
680680
(True, True)
681681
])
682-
def test_merge_sets_duplication_allowance_flag(allow_duplication_on_left, allow_duplication_on_right):
682+
@pytest.mark.parametrize(["how"], [
683+
("left",),
684+
("right",),
685+
("inner",),
686+
("outer",),
687+
("left_anti",),
688+
("right_anti",),
689+
("cross",),
690+
])
691+
def test_merge_sets_duplication_allowance_flag(how, allow_duplication_on_left, allow_duplication_on_right):
683692
"""
684-
Check that pandas.merge correctly sets the allow_duplicate_labels flag
693+
Check that DataFrame.merge correctly sets the allow_duplicate_labels flag
685694
on its result.
686695
687-
If one or both of the arguments to merge has its flag set to False,
688-
then the result of merge should have its flag set to False.
689-
Otherwise, the result should have its flag set to True.
696+
The flag on the result should be set to true if and only if both arguments to merge
697+
have their flags set to True.
690698
"""
691699
# Arrange
692700
left = pd.DataFrame({"test": [1]}).set_flags(allows_duplicate_labels=allow_duplication_on_left)
693701
right = pd.DataFrame({"test": [1]}).set_flags(allows_duplicate_labels=allow_duplication_on_right)
694702

695703
# Act
696-
result = left.merge(right, how="inner", on="test")
704+
if not how == "cross":
705+
result = left.merge(right, how=how, on="test")
706+
else:
707+
result = left.merge(right, how=how)
708+
709+
# Assert
710+
expected_duplication_allowance = allow_duplication_on_left and allow_duplication_on_right
711+
assert result.flags.allows_duplicate_labels == expected_duplication_allowance
712+
713+
@pytest.mark.parametrize(["allow_duplication_on_left", "allow_duplication_on_right"],
714+
[
715+
(False, False),
716+
(False, True),
717+
(True, False),
718+
(True, True)
719+
])
720+
def test_merge_asof_sets_duplication_allowance_flag(allow_duplication_on_left, allow_duplication_on_right):
721+
"""
722+
Check that pandas.merge_asof correctly sets the allow_duplicate_labels flag
723+
on its result.
724+
725+
The flag on the result should be set to true if and only if both arguments to merge_asof
726+
have their flags set to True.
727+
"""
728+
# Arrange
729+
left = pd.DataFrame({"test": [1]}).set_flags(allows_duplicate_labels=allow_duplication_on_left)
730+
right = pd.DataFrame({"test": [1]}).set_flags(allows_duplicate_labels=allow_duplication_on_right)
731+
732+
# Act
733+
result = pd.merge_asof(left, right)
697734

698735
# Assert
699736
expected_duplication_allowance = allow_duplication_on_left and allow_duplication_on_right

0 commit comments

Comments
 (0)