Skip to content

Commit 66e22c5

Browse files
authored
Refactor test_combine_first dataframe (#63295)
1 parent 47fea80 commit 66e22c5

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

pandas/tests/frame/methods/test_combine_first.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ def test_combine_first_mixed(self):
3030
combined = f.combine_first(g)
3131
tm.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):
3534
head, tail = float_frame[:5], float_frame[5:]
36-
3735
combined = head.combine_first(tail)
3836
reordered_frame = float_frame.reindex(combined.index)
37+
3938
tm.assert_frame_equal(combined, reordered_frame)
4039
tm.assert_index_equal(combined.columns, float_frame.columns)
4140
tm.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):
4446
fcopy = float_frame.copy()
4547
fcopy["A"] = 1
4648
del fcopy["C"]
@@ -56,36 +58,36 @@ def test_combine_first(self, float_frame):
5658
tm.assert_series_equal(combined["C"], fcopy2["C"])
5759
tm.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()
6165
head["A"] = 1
62-
6366
combined = head.combine_first(tail)
6467
assert (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+
6774
tail.iloc[:10, tail.columns.get_loc("A")] = 0
6875
combined = tail.combine_first(head)
6976
assert (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):
7979
comb = float_frame.combine_first(DataFrame())
8080
tm.assert_frame_equal(comb, float_frame)
8181

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

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

88-
# #2525
89+
def test_combine_first_column_union(self):
90+
# GH#2525
8991
df = DataFrame({"a": [1]}, index=[datetime(2012, 1, 1)])
9092
df2 = DataFrame(columns=["b"])
9193
result = df.combine_first(df2)

0 commit comments

Comments
 (0)