@@ -24,6 +24,12 @@ def test_combine_first_mixed(self):
24
24
combined = f .combine_first (g )
25
25
tm .assert_frame_equal (combined , exp )
26
26
27
+ exp = DataFrame (
28
+ {"A" : list ("abab" ), "B" : [0 , 1 , 0 , 1 ]}, index = [0 , 1 , 5 , 6 ]
29
+ )
30
+ combined = f .combine_first (g , preserve_dtypes = True )
31
+ tm .assert_frame_equal (combined , exp )
32
+
27
33
def test_combine_first (self , float_frame ):
28
34
# disjoint
29
35
head , tail = float_frame [:5 ], float_frame [5 :]
@@ -363,9 +369,16 @@ def test_combine_first_int(self):
363
369
expected_12 = DataFrame ({"a" : [0 , 1 , 3 , 5 ]}, dtype = "float64" )
364
370
tm .assert_frame_equal (result_12 , expected_12 )
365
371
372
+ result_12 = df1 .combine_first (df2 , preserve_dtypes = True )
373
+ expected_12 = DataFrame ({"a" : [0 , 1 , 3 , 5 ]})
374
+ tm .assert_frame_equal (result_12 , expected_12 )
375
+
366
376
result_21 = df2 .combine_first (df1 )
367
377
expected_21 = DataFrame ({"a" : [1 , 4 , 3 , 5 ]}, dtype = "float64" )
378
+ tm .assert_frame_equal (result_21 , expected_21 )
368
379
380
+ result_21 = df2 .combine_first (df1 , preserve_dtypes = True )
381
+ expected_21 = DataFrame ({"a" : [1 , 4 , 3 , 5 ]})
369
382
tm .assert_frame_equal (result_21 , expected_21 )
370
383
371
384
@pytest .mark .parametrize ("val" , [1 , 1.0 ])
@@ -439,3 +452,34 @@ def test_combine_first_with_nan_multiindex():
439
452
index = mi_expected ,
440
453
)
441
454
tm .assert_frame_equal (res , expected )
455
+
456
+ def test_combine_preserve_dtypes ():
457
+ a = Series (["a" , "b" ], index = range (2 ))
458
+ b = Series (range (2 ), index = range (2 ))
459
+ f = DataFrame ({"A" : a , "B" : b })
460
+
461
+ c = Series (["a" , "b" ], index = range (5 , 7 ))
462
+ b = Series (range (- 1 , 1 ), index = range (5 , 7 ))
463
+ g = DataFrame ({"B" : b , "C" : c })
464
+
465
+ exp = DataFrame (
466
+ {
467
+ "A" : ["a" , "b" , np .nan , np .nan ],
468
+ "B" : [0.0 , 1.0 , - 1.0 , 0.0 ],
469
+ "C" : [np .nan , np .nan , "a" , "b" ]
470
+ },
471
+ index = [0 , 1 , 5 , 6 ]
472
+ )
473
+ combined = f .combine_first (g )
474
+ tm .assert_frame_equal (combined , exp )
475
+
476
+ exp = DataFrame (
477
+ {
478
+ "A" : ["a" , "b" , np .nan , np .nan ],
479
+ "B" : [0 , 1 , - 1 , 0 ],
480
+ "C" : [np .nan , np .nan , "a" , "b" ]
481
+ },
482
+ index = [0 , 1 , 5 , 6 ]
483
+ )
484
+ combined = f .combine_first (g , preserve_dtypes = True )
485
+ tm .assert_frame_equal (combined , exp )
0 commit comments