Skip to content

Commit f2abf1f

Browse files
committed
Resolved several issues in test_finalize.
1 parent 9b51d3e commit f2abf1f

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

pandas/tests/generic/test_finalize.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
"""An exhaustive list of pandas methods exercising NDFrame.__finalize__."""
1+
"""
2+
An exhaustive list of pandas methods exercising NDFrame.__finalize__.
3+
"""
24

5+
from copy import deepcopy
36
import operator
47
import re
58

@@ -363,7 +366,8 @@ def idfn(x):
363366
m = xpr.search(str(x))
364367
if m:
365368
return m.group(1)
366-
return str(x)
369+
else:
370+
return str(x)
367371

368372

369373
@pytest.mark.parametrize("ndframe_method", _all_methods, ids=lambda x: idfn(x[-1]))
@@ -672,7 +676,7 @@ def test_finalize_frame_series_name():
672676

673677

674678
# ----------------------------------------------------------------------------
675-
# Tests for merge
679+
# Merge
676680

677681

678682
@pytest.mark.parametrize(
@@ -696,9 +700,6 @@ def test_merge_correctly_sets_duplication_allowance_flag(
696700
allow_on_left: bool,
697701
allow_on_right: bool,
698702
):
699-
"""Check that DataFrame.merge correctly sets the allow_duplicate_labels flag
700-
on its result.
701-
"""
702703
left = pd.DataFrame({"test": [1]}).set_flags(allows_duplicate_labels=allow_on_left)
703704
right = pd.DataFrame({"test": [1]}).set_flags(
704705
allows_duplicate_labels=allow_on_right,
@@ -733,18 +734,21 @@ def test_merge_asof_correctly_sets_duplication_allowance_flag(
733734

734735

735736
def test_merge_propagates_metadata_from_equal_input_metadata():
736-
metadata = {"a": 2}
737+
metadata = {"a": [1, 2]}
737738
left = pd.DataFrame({"test": [1]})
738739
left.attrs = metadata
739740
right = pd.DataFrame({"test": [1]})
740-
right.attrs = metadata.copy()
741+
right.attrs = deepcopy(metadata)
741742

742743
result = left.merge(right, how="inner", on="test")
743744

744745
assert result.attrs == metadata
746+
745747
# Verify that merge deep-copies the attr dictionary.
746-
left.attrs = {"b": 3}
747-
assert result.attrs == metadata
748+
assert result.attrs is not left.attrs
749+
assert result.attrs is not right.attrs
750+
assert result.attrs["a"] is not left.attrs["a"]
751+
assert result.attrs["a"] is not right.attrs["a"]
748752

749753

750754
def test_merge_does_not_propagate_metadata_from_unequal_input_metadata():
@@ -758,26 +762,33 @@ def test_merge_does_not_propagate_metadata_from_unequal_input_metadata():
758762
assert result.attrs == {}
759763

760764

761-
no_metadata = pd.DataFrame({"test": [1]})
762-
763-
has_metadata = pd.DataFrame({"test": [1]})
764-
has_metadata.attrs = {"a": 2}
765-
766-
767765
@pytest.mark.parametrize(
768-
["left", "right", "expected"],
766+
["left_has_metadata", "right_has_metadata", "expected"],
769767
[
770-
(no_metadata, has_metadata, {}),
771-
(has_metadata, no_metadata, {}),
772-
(no_metadata, no_metadata, {}),
768+
(False, True, {}),
769+
(True, False, {}),
770+
(False, False, {}),
773771
],
774772
ids=["left-empty", "right-empty", "both-empty"],
775773
)
776774
def test_merge_does_not_propagate_metadata_if_one_input_has_no_metadata(
777-
left: pd.DataFrame,
778-
right: pd.DataFrame,
775+
left_has_metadata: bool,
776+
right_has_metadata: bool,
779777
expected: dict,
780778
):
779+
left = pd.DataFrame({"test": [1]})
780+
right = pd.DataFrame({"test": [1]})
781+
782+
if left_has_metadata:
783+
left.attrs = {"a": [1, 2]}
784+
else:
785+
left.attrs = {}
786+
787+
if right_has_metadata:
788+
right.attrs = {"a": [1, 2]}
789+
else:
790+
right.attrs = {}
791+
781792
result = left.merge(right, how="inner", on="test")
782793

783794
assert result.attrs == expected

0 commit comments

Comments
 (0)