1616import pandas ._testing as tm
1717
1818
19+ @pytest .fixture
20+ def reordered_frame (float_frame ):
21+ head , tail = float_frame [:5 ], float_frame [5 :]
22+ combined = head .combine_first (tail )
23+ reordered_frame = float_frame .reindex (combined .index )
24+ return reordered_frame
25+
26+
1927class TestDataFrameCombineFirst :
2028 def test_combine_first_mixed (self ):
2129 a = Series (["a" , "b" ], index = range (2 ))
@@ -30,17 +38,18 @@ def test_combine_first_mixed(self):
3038 combined = f .combine_first (g )
3139 tm .assert_frame_equal (combined , exp )
3240
33- def test_combine_first (self , float_frame ):
34- # disjoint
41+ def test_combine_first_disjoint (self , float_frame ):
3542 head , tail = float_frame [:5 ], float_frame [5 :]
36-
3743 combined = head .combine_first (tail )
3844 reordered_frame = float_frame .reindex (combined .index )
45+
3946 tm .assert_frame_equal (combined , reordered_frame )
4047 tm .assert_index_equal (combined .columns , float_frame .columns )
4148 tm .assert_series_equal (combined ["A" ], reordered_frame ["A" ])
49+ tm .assert_series_equal (combined ["A" ].reindex (head .index ), head ["A" ])
50+ tm .assert_series_equal (combined ["A" ].reindex (tail .index ), tail ["A" ])
4251
43- # same index
52+ def test_combine_first_same_index ( self , float_frame ):
4453 fcopy = float_frame .copy ()
4554 fcopy ["A" ] = 1
4655 del fcopy ["C" ]
@@ -56,36 +65,31 @@ def test_combine_first(self, float_frame):
5665 tm .assert_series_equal (combined ["C" ], fcopy2 ["C" ])
5766 tm .assert_series_equal (combined ["D" ], fcopy ["D" ])
5867
59- # overlap
60- head , tail = reordered_frame [:10 ].copy (), reordered_frame
68+ def test_combine_first_overlap ( self , reordered_frame ):
69+ head , tail = reordered_frame [:10 ].copy (), reordered_frame . copy ()
6170 head ["A" ] = 1
62-
6371 combined = head .combine_first (tail )
6472 assert (combined ["A" ][:10 ] == 1 ).all ()
6573
66- # reverse overlap
74+ def test_combine_first_reverse_overlap (self , reordered_frame ):
75+ head , tail = reordered_frame [:10 ].copy (), reordered_frame .copy ()
6776 tail .iloc [:10 , tail .columns .get_loc ("A" )] = 0
6877 combined = tail .combine_first (head )
6978 assert (combined ["A" ][:10 ] == 0 ).all ()
7079
71- # no overlap
72- f = float_frame [:10 ]
73- g = float_frame [10 :]
74- combined = f .combine_first (g )
75- tm .assert_series_equal (combined ["A" ].reindex (f .index ), f ["A" ])
76- tm .assert_series_equal (combined ["A" ].reindex (g .index ), g ["A" ])
77-
78- # corner cases
80+ def test_combine_first_with_empty (self , float_frame ):
7981 comb = float_frame .combine_first (DataFrame ())
8082 tm .assert_frame_equal (comb , float_frame )
8183
8284 comb = DataFrame ().combine_first (float_frame )
8385 tm .assert_frame_equal (comb , float_frame .sort_index ())
8486
87+ def test_combine_first_with_new_index (self , float_frame ):
8588 comb = float_frame .combine_first (DataFrame (index = ["faz" , "boo" ]))
8689 assert "faz" in comb .index
8790
88- # #2525
91+ def test_combine_first_column_union (self ):
92+ # GH#2525
8993 df = DataFrame ({"a" : [1 ]}, index = [datetime (2012 , 1 , 1 )])
9094 df2 = DataFrame (columns = ["b" ])
9195 result = df .combine_first (df2 )
0 commit comments