How to join two data files by date and time index

x_index=pd.date_range(dt.date(2010,1,1),dt.date(2010,1,5))
y_index=pd.date_range(dt.date(2010,1,2),dt.date(2010,1,6))
x = pd.DataFrame({"AAPL":[1,2,3,4,5]}, index=x_index)
y = pd.DataFrame({"GE": [1,2,3,4,5]}, index=y_index)

The result should be:

            AAPL GE
2010-01-01   1   nan
2010-01-02   2   1
2010-01-03   3   2
2010-01-04   4   3
2010-01-05   5   4
2010-01-06   nan     5
+4
source share
1 answer

Since you do not have a common column, you need to specify whether to use the indexes of both data files and that you want to perform an β€œexternal” merge:

In [226]:

x.merge(y, how='outer', left_index=True, right_index=True)
Out[226]:
            AAPL  GE
2010-01-01     1 NaN
2010-01-02     2   1
2010-01-03     3   2
2010-01-04     4   3
2010-01-05     5   4
2010-01-06   NaN   5

[6 rows x 2 columns]
+9
source

Source: https://habr.com/ru/post/1533930/


All Articles