Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two time series in pandas

Apologies if this is obviously documented somewhere, but I'm having trouble discovering it. I have two TimeSeries with some overlapping dates/indices and I'd like to merge them. I assume I'll have to specify which of the two series to take the values from for the overlapping dates. For illustration I have:

s1:
2008-09-15    100
2008-10-15    101

s2:
2008-10-15    101.01
2008-11-15    102.02

and I want:

s3:
2008-09-15    100
2008-10-15    101
2008-11-15    102.02

or

s3:
2008-09-15    100
2008-10-15    101.01
2008-11-15    102.02
like image 449
user1507844 Avatar asked Jun 05 '13 16:06

user1507844


People also ask

How do you combine two series of time?

To merge two time series in R, we use the ts() function but as parameter data, we pass a vector that contains all the time series to be merged. Example: Here, we have created two-time series and merged them using the above syntax.

How do I merge two series in pandas?

To combine two series into a DataFrame in Pandas, we can take two series and concatenate them using concat() method.

Can you merge a series to a DataFrame pandas?

Let's say you already have a pandas DataFrame with few columns and you would like to add/merge Series as columns into existing DataFrame, this is certainly possible using pandas. Dataframe. merge() method.

Can you combine two Dataframes in pandas?

Pandas' merge and concat can be used to combine subsets of a DataFrame, or even data from different files. join function combines DataFrames based on index or column. Joining two DataFrames can be done in multiple ways (left, right, and inner) depending on what data must be in the final DataFrame.


1 Answers

This can be achieved using combine_first:

In [11]: s1.combine_first(s2)
Out[11]:
2008-09-15    100.00
2008-10-15    101.00
2008-11-15    102.02
dtype: float64
like image 60
user1507844 Avatar answered Oct 17 '22 22:10

user1507844