@@ -30,17 +30,19 @@ def test_combine_first_mixed(self):
3030combined = f .combine_first (g )
3131tm .assert_frame_equal (combined , exp )
3232
33- def test_combine_first (self , float_frame ):
34- # disjoint
33+ def test_combine_first_disjoint (self , float_frame ):
3534head , tail = float_frame [:5 ], float_frame [5 :]
36-
3735combined = head .combine_first (tail )
3836reordered_frame = float_frame .reindex (combined .index )
37+
3938tm .assert_frame_equal (combined , reordered_frame )
4039tm .assert_index_equal (combined .columns , float_frame .columns )
4140tm .assert_series_equal (combined ["A" ], reordered_frame ["A" ])
4241
43- # same index
42+ tm .assert_series_equal (combined ["A" ].reindex (head .index ), head ["A" ])
43+ tm .assert_series_equal (combined ["A" ].reindex (tail .index ), tail ["A" ])
44+
45+ def test_combine_first_same_index (self , float_frame ):
4446fcopy = float_frame .copy ()
4547fcopy ["A" ] = 1
4648del fcopy ["C" ]
@@ -56,36 +58,36 @@ def test_combine_first(self, float_frame):
5658tm .assert_series_equal (combined ["C" ], fcopy2 ["C" ])
5759tm .assert_series_equal (combined ["D" ], fcopy ["D" ])
5860
59- # overlap
60- head , tail = reordered_frame [:10 ].copy (), reordered_frame
61+ def test_combine_first_overlap (self , float_frame ):
62+ combined = float_frame [:5 ].combine_first (float_frame [5 :])
63+ reordered_frame = float_frame .reindex (combined .index )
64+ head , tail = reordered_frame [:10 ].copy (), reordered_frame .copy ()
6165head ["A" ] = 1
62-
6366combined = head .combine_first (tail )
6467assert (combined ["A" ][:10 ] == 1 ).all ()
6568
66- # reverse overlap
69+ def test_combine_first_reverse_overlap (self , float_frame ):
70+ combined = float_frame [:5 ].combine_first (float_frame [5 :])
71+ reordered_frame = float_frame .reindex (combined .index )
72+ head , tail = reordered_frame [:10 ].copy (), reordered_frame
73+
6774tail .iloc [:10 , tail .columns .get_loc ("A" )] = 0
6875combined = tail .combine_first (head )
6976assert (combined ["A" ][:10 ] == 0 ).all ()
7077
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
78+ def test_combine_first_with_empty (self , float_frame ):
7979comb = float_frame .combine_first (DataFrame ())
8080tm .assert_frame_equal (comb , float_frame )
8181
8282comb = DataFrame ().combine_first (float_frame )
8383tm .assert_frame_equal (comb , float_frame .sort_index ())
8484
85+ def test_combine_first_with_new_index (self , float_frame ):
8586comb = float_frame .combine_first (DataFrame (index = ["faz" , "boo" ]))
8687assert "faz" in comb .index
8788
88- # #2525
89+ def test_combine_first_column_union (self ):
90+ # GH#2525
8991df = DataFrame ({"a" : [1 ]}, index = [datetime (2012 , 1 , 1 )])
9092df2 = DataFrame (columns = ["b" ])
9193result = df .combine_first (df2 )
0 commit comments