Repository: pandas-dev/pandas
File: pandas/tests/frame/methods/test_combine_first.py (Python)

Code before:
        tm.assert_frame_equal(combined, reordered_frame)
        tm.assert_index_equal(combined.columns, float_frame.columns)
        tm.assert_series_equal(combined["A"], reordered_frame["A"])
        tm.assert_series_equal(combined["A"].reindex(head.index), head["A"])
        tm.assert_series_equal(combined["A"].reindex(tail.index), tail["A"])

    def test_combine_first_same_index(self, float_frame):
        fcopy = float_frame.copy()
        fcopy["A"] = 1
        del fcopy["C"]

        fcopy2 = float_frame.copy()
        fcopy2["B"] = 0
        del fcopy2["D"]

        combined = fcopy.combine_first(fcopy2)

        assert (combined["A"] == 1).all()
        tm.assert_series_equal(combined["B"], fcopy["B"])
        tm.assert_series_equal(combined["C"], fcopy2["C"])
        tm.assert_series_equal(combined["D"], fcopy["D"])

    def test_combine_first_overlap(self, reordered_frame):
        head, tail = reordered_frame[:10].copy(), reordered_frame.copy()
        head["A"] = 1
        combined = head.combine_first(tail)
        assert (combined["A"][:10] == 1).all()

    def test_combine_first_reverse_overlap(self, reordered_frame):
        head, tail = reordered_frame[:10].copy(), reordered_frame.copy()
        tail.iloc[:10, tail.columns.get_loc("A")] = 0
        combined = tail.combine_first(head)
        assert (combined["A"][:10] == 0).all()

    def test_combine_first_with_empty(self, float_frame):
        comb = float_frame.combine_first(DataFrame())
        tm.assert_frame_equal(comb, float_frame)

        comb = DataFrame().combine_first(float_frame)
        tm.assert_frame_equal(comb, float_frame.sort_index())

    def test_combine_first_with_new_index(self, float_frame):
        comb = float_frame.combine_first(DataFrame(index=["faz", "boo"]))
        assert "faz" in comb.index

    def test_combine_first_column_union(self):
        # GH#2525
        df = DataFrame({"a": [1]}, index=[datetime(2012, 1, 1)])
        df2 = DataFrame(columns=["b"])
        result = df.combine_first(df2)
        assert "b" in result

Review comment (suggestion):
The no overlap case had the same setup as the disjoint case, just with assertions that had a different focus, so I moved those assertions up into test_combine_first_disjoint and then removed the reverse overlap code.

Code after:
        fcopy = float_frame.copy()
        fcopy["A"] = 1
        del fcopy["C"]

        fcopy2 = float_frame.copy()
        fcopy2["B"] = 0
        del fcopy2["D"]

        combined = fcopy.combine_first(fcopy2)

        assert (combined["A"] == 1).all()
        tm.assert_series_equal(combined["B"], fcopy["B"])
        tm.assert_series_equal(combined["C"], fcopy2["C"])
        tm.assert_series_equal(combined["D"], fcopy["D"])

    def test_combine_first_overlap(self, float_frame):
        combined = float_frame[:5].combine_first(float_frame[5:])
        reordered_frame = float_frame.reindex(combined.index)
        head, tail = reordered_frame[:10].copy(), reordered_frame.copy()
        head["A"] = 1
        combined = head.combine_first(tail)
        assert (combined["A"][:10] == 1).all()

    def test_combine_first_reverse_overlap(self, float_frame):
        combined = float_frame[:5].combine_first(float_frame[5:])
        reordered_frame = float_frame.reindex(combined.index)
        head, tail = reordered_frame[:10].copy(), reordered_frame

        tail.iloc[:10, tail.columns.get_loc("A")] = 0
        combined = tail.combine_first(head)
        assert (combined["A"][:10] == 0).all()

    def test_combine_first_with_empty(self, float_frame):
        comb = float_frame.combine_first(DataFrame())
        tm.assert_frame_equal(comb, float_frame)

        comb = DataFrame().combine_first(float_frame)
        tm.assert_frame_equal(comb, float_frame.sort_index())

    def test_combine_first_with_new_index(self, float_frame):
        comb = float_frame.combine_first(DataFrame(index=["faz", "boo"]))
        assert "faz" in comb.index

    def test_combine_first_column_union(self):
        # GH#2525
        df = DataFrame({"a": [1]}, index=[datetime(2012, 1, 1)])
        df2 = DataFrame(columns=["b"])
        result = df.combine_first(df2)
        assert "b" in result

    def test_combine_first_mixed_bug(self):