Skip to content

Commit 742a268

Browse files
committed
Activated test for metadata of merge operation.
1 parent ef4a9c3 commit 742a268

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

pandas/core/reshape/merge.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,9 @@ def _reindex_and_concat(
11291129
return result
11301130

11311131
def get_result(self) -> DataFrame:
1132+
"""
1133+
Execute the merge.
1134+
"""
11321135
if self.indicator:
11331136
self.left, self.right = self._indicator_pre_merge(self.left, self.right)
11341137

@@ -1148,7 +1151,7 @@ def get_result(self) -> DataFrame:
11481151
self._maybe_restore_index_levels(result)
11491152

11501153
return result.__finalize__(
1151-
types.SimpleNamespace(input_objs=[self.left, self.right]), method="merge"
1154+
self.left, method="merge"
11521155
)
11531156

11541157
@final
@@ -1167,6 +1170,12 @@ def _indicator_name(self) -> str | None:
11671170
def _indicator_pre_merge(
11681171
self, left: DataFrame, right: DataFrame
11691172
) -> tuple[DataFrame, DataFrame]:
1173+
"""
1174+
Add one indicator column to each of the left and right inputs to a merge operation.
1175+
1176+
These columns are used to produce another column in the output of the merge, indicating
1177+
for each row of the output whether it was produced using the left, right or both inputs.
1178+
"""
11701179
columns = left.columns.union(right.columns)
11711180

11721181
for i in ["_left_indicator", "_right_indicator"]:
@@ -1193,6 +1202,12 @@ def _indicator_pre_merge(
11931202

11941203
@final
11951204
def _indicator_post_merge(self, result: DataFrame) -> DataFrame:
1205+
"""
1206+
Add an indicator column to the merge result.
1207+
1208+
This column indicates for each row of the output whether it was produced using the left,
1209+
right or both inputs.
1210+
"""
11961211
result["_left_indicator"] = result["_left_indicator"].fillna(0)
11971212
result["_right_indicator"] = result["_right_indicator"].fillna(0)
11981213

pandas/tests/generic/test_finalize.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,7 @@
148148
operator.methodcaller("melt", id_vars=["A"], value_vars=["B"]),
149149
),
150150
(pd.DataFrame, frame_data, operator.methodcaller("map", lambda x: x)),
151-
pytest.param(
152-
(
153-
pd.DataFrame,
154-
frame_data,
155-
operator.methodcaller("merge", pd.DataFrame({"A": [1]})),
156-
),
157-
marks=not_implemented_mark,
158-
),
151+
(pd.DataFrame, frame_data, operator.methodcaller("merge", pd.DataFrame({"A": [1]}))),
159152
(pd.DataFrame, frame_data, operator.methodcaller("round", 2)),
160153
(pd.DataFrame, frame_data, operator.methodcaller("corr")),
161154
pytest.param(
@@ -675,3 +668,35 @@ def test_finalize_frame_series_name():
675668
df = pd.DataFrame({"name": [1, 2]})
676669
result = pd.Series([1, 2]).__finalize__(df)
677670
assert result.name is None
671+
672+
# ----------------------------------------------------------------------------
673+
# Merge tests
674+
675+
@pytest.mark.parametrize(["allow_duplication_on_left", "allow_duplication_on_right"],
676+
[
677+
(False, False),
678+
(False, True),
679+
(True, False),
680+
(True, True)
681+
])
682+
def test_merge_sets_duplication_allowance_flag(allow_duplication_on_left, allow_duplication_on_right):
683+
"""
684+
Check that pandas.merge correctly sets the allow_duplicate_labels flag
685+
on its result.
686+
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.
690+
"""
691+
# Arrange
692+
left = pd.DataFrame({"test": [1]})
693+
left.set_flags(allows_duplicate_labels=allow_duplication_on_left)
694+
right = pd.DataFrame({"test": [1]})
695+
right.set_flags(allows_duplicate_labels=allow_duplication_on_right)
696+
697+
# Act
698+
result = left.merge(right, how="inner", on="test")
699+
700+
# Assert
701+
expected_duplication_allowance = allow_duplication_on_left and allow_duplication_on_right
702+
assert result.flags.allows_duplicate_labels == expected_duplication_allowance

0 commit comments

Comments
 (0)