Skip to content Skip to sidebar Skip to footer

Concat Pandas DataFrame Along Timeseries Indexes

I have two largish (snippets provided) pandas DateFrames with unequal dates as indexes that I wish to concat into one: NAB.AX CBA.AX

Solution 1:

It is possible to read the data with pandas and to concatenate it.

First import the data

In [449]: import pandas.io.data as web

In [450]: nab = web.get_data_yahoo('NAB.AX', start='2009-05-25',
                                   end='2009-06-05')[['Close', 'Volume']]

In [451]: cba = web.get_data_yahoo('CBA.AX', start='2009-05-26',
                                   end='2009-06-08')[['Close', 'Volume']]

In [453]: nab
Out[453]: 
            Close    Volume
Date                       
2009-05-25  21.15   9685100
2009-05-26  21.64   8541900
2009-05-27  21.74   9042900
2009-05-28  21.63   9701000
2009-05-29  22.02  14665700
2009-06-01  22.52   6782000
2009-06-02  22.80  10473400
2009-06-03  23.11   9931400
2009-06-04  22.21  17869000
2009-06-05  21.95   8214300

In [454]: cba
Out[454]: 
            Close    Volume
Date                       
2009-05-26  35.45   4529600
2009-05-27  35.13   4521500
2009-05-28  33.95   7945400
2009-05-29  35.14  12548500
2009-06-01  36.16   4509400
2009-06-02  36.33   4304900
2009-06-03  36.80   4845400
2009-06-04  36.79   4592300
2009-06-05  36.51   4417500
2009-06-08  36.51         0

Than concatenate it:

In [455]: keys = ['CBA.AX','NAB.AX']

In [456]: pd.concat([cba, nab], axis=1, keys=keys)
Out[456]: 
            CBA.AX            NAB.AX          
             Close    Volume   Close    Volume
Date                                          
2009-05-25     NaN       NaN   21.15   9685100
2009-05-26   35.45   4529600   21.64   8541900
2009-05-27   35.13   4521500   21.74   9042900
2009-05-28   33.95   7945400   21.63   9701000
2009-05-29   35.14  12548500   22.02  14665700
2009-06-01   36.16   4509400   22.52   6782000
2009-06-02   36.33   4304900   22.80  10473400
2009-06-03   36.80   4845400   23.11   9931400
2009-06-04   36.79   4592300   22.21  17869000
2009-06-05   36.51   4417500   21.95   8214300
2009-06-08   36.51         0     NaN       NaN

Solution 2:

Try to join on outer.

When I am working with a number of stocks, I would usually have a frame titled "open high,low,close,etc" with column as a ticker. If you want one data structure, I would use Panels for this.

for Yahoo data, you can use pandas:

import pandas.io.data as data
spy = data.DataReader("SPY","yahoo","1991/1/1")

Post a Comment for "Concat Pandas DataFrame Along Timeseries Indexes"