1from datetime import datetime 2 3import numpy as np 4 5import pandas as pd 6from pandas import Period, Series, date_range, period_range, to_datetime 7import pandas._testing as tm 8 9 10class TestCombineFirst: 11 def test_combine_first_period_datetime(self): 12 # GH#3367 13 didx = date_range(start="1950-01-31", end="1950-07-31", freq="M") 14 pidx = period_range(start=Period("1950-1"), end=Period("1950-7"), freq="M") 15 # check to be consistent with DatetimeIndex 16 for idx in [didx, pidx]: 17 a = Series([1, np.nan, np.nan, 4, 5, np.nan, 7], index=idx) 18 b = Series([9, 9, 9, 9, 9, 9, 9], index=idx) 19 20 result = a.combine_first(b) 21 expected = Series([1, 9, 9, 4, 5, 9, 7], index=idx, dtype=np.float64) 22 tm.assert_series_equal(result, expected) 23 24 def test_combine_first_name(self, datetime_series): 25 result = datetime_series.combine_first(datetime_series[:5]) 26 assert result.name == datetime_series.name 27 28 def test_combine_first(self): 29 values = tm.makeIntIndex(20).values.astype(float) 30 series = Series(values, index=tm.makeIntIndex(20)) 31 32 series_copy = series * 2 33 series_copy[::2] = np.NaN 34 35 # nothing used from the input 36 combined = series.combine_first(series_copy) 37 38 tm.assert_series_equal(combined, series) 39 40 # Holes filled from input 41 combined = series_copy.combine_first(series) 42 assert np.isfinite(combined).all() 43 44 tm.assert_series_equal(combined[::2], series[::2]) 45 tm.assert_series_equal(combined[1::2], series_copy[1::2]) 46 47 # mixed types 48 index = tm.makeStringIndex(20) 49 floats = Series(tm.randn(20), index=index) 50 strings = Series(tm.makeStringIndex(10), index=index[::2]) 51 52 combined = strings.combine_first(floats) 53 54 tm.assert_series_equal(strings, combined.loc[index[::2]]) 55 tm.assert_series_equal(floats[1::2].astype(object), combined.loc[index[1::2]]) 56 57 # corner case 58 ser = Series([1.0, 2, 3], index=[0, 1, 2]) 59 empty = Series([], index=[], dtype=object) 60 result = ser.combine_first(empty) 61 ser.index = ser.index.astype("O") 62 tm.assert_series_equal(ser, result) 63 64 def test_combine_first_dt64(self): 65 66 s0 = to_datetime(Series(["2010", np.NaN])) 67 s1 = to_datetime(Series([np.NaN, "2011"])) 68 rs = s0.combine_first(s1) 69 xp = to_datetime(Series(["2010", "2011"])) 70 tm.assert_series_equal(rs, xp) 71 72 s0 = to_datetime(Series(["2010", np.NaN])) 73 s1 = Series([np.NaN, "2011"]) 74 rs = s0.combine_first(s1) 75 xp = Series([datetime(2010, 1, 1), "2011"]) 76 tm.assert_series_equal(rs, xp) 77 78 def test_combine_first_dt_tz_values(self, tz_naive_fixture): 79 ser1 = Series( 80 pd.DatetimeIndex(["20150101", "20150102", "20150103"], tz=tz_naive_fixture), 81 name="ser1", 82 ) 83 ser2 = Series( 84 pd.DatetimeIndex(["20160514", "20160515", "20160516"], tz=tz_naive_fixture), 85 index=[2, 3, 4], 86 name="ser2", 87 ) 88 result = ser1.combine_first(ser2) 89 exp_vals = pd.DatetimeIndex( 90 ["20150101", "20150102", "20150103", "20160515", "20160516"], 91 tz=tz_naive_fixture, 92 ) 93 exp = Series(exp_vals, name="ser1") 94 tm.assert_series_equal(exp, result) 95