问题
I have two disjoint time series objects, for example
-ts1
Date Price
2010-01-01 1800.0
2010-01-04 1500.0
2010-01-08 1600.0
2010-01-09 1400.0
Name: Price, dtype: float64
-ts2
Date Price
2010-01-02 2000.0
2010-01-03 2200.0
2010-01-05 2010.0
2010-01-07 2100.0
2010-01-10 2110.0
How I could merge the two into a single time series that should be sorted on date? like
-ts3
Date Price
2010-01-01 1800.0
2010-01-02 2000.0
2010-01-03 2200.0
2010-01-04 1500.0
2010-01-05 2010.0
2010-01-07 2100.0
2010-01-08 1600.0
2010-01-09 1400.0
2010-01-10 2110.0
回答1:
Use pandas.concat or DataFrame.append for join together and then DataFrame.sort_values by column Date
, last for default indices DataFrame.reset_index with parameter drop=True
:
df3 = pd.concat([df1, df2]).sort_values('Date').reset_index(drop=True)
Alternative:
df3 = df1.append(df2).sort_values('Date').reset_index(drop=True)
print (df3)
Date Price
0 2010-01-01 1800.0
1 2010-01-02 2000.0
2 2010-01-03 2200.0
3 2010-01-04 1500.0
4 2010-01-05 2010.0
5 2010-01-07 2100.0
6 2010-01-08 1600.0
7 2010-01-09 1400.0
8 2010-01-10 2110.0
EDIT:
If TimeSeries then solution is simplify:
s3= pd.concat([s1, s2]).sort_index()
回答2:
You can set the index of each to 'Date'
and use combine_first
ts1.set_index('Date').combine_first(ts2.set_index('Date')).reset_index()
Date Price
0 2010-01-01 1800.0
1 2010-01-02 2000.0
2 2010-01-03 2200.0
3 2010-01-04 1500.0
4 2010-01-05 2010.0
5 2010-01-07 2100.0
6 2010-01-08 1600.0
7 2010-01-09 1400.0
8 2010-01-10 2110.0
If these were Series in the first place, then you could simply
ts1.combine_first(ts2)
来源:https://stackoverflow.com/questions/49584917/how-to-merge-two-pandas-time-series-objects-with-different-date-time-indices