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
Date2009-05-2521.1596851002009-05-2621.6485419002009-05-2721.7490429002009-05-2821.6397010002009-05-2922.02146657002009-06-0122.5267820002009-06-0222.80104734002009-06-0323.1199314002009-06-0422.21178690002009-06-0521.958214300In [454]: cba
Out[454]: 
            Close    Volume
Date2009-05-2635.4545296002009-05-2735.1345215002009-05-2833.9579454002009-05-2935.14125485002009-06-0136.1645094002009-06-0236.3343049002009-06-0336.8048454002009-06-0436.7945923002009-06-0536.5144175002009-06-0836.510

Than concatenate it:

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

In [456]:pd.concat([cba,nab],axis=1,keys=keys)Out[456]:CBA.AXNAB.AXCloseVolumeCloseVolumeDate2009-05-25     NaNNaN21.1596851002009-05-26   35.45452960021.6485419002009-05-27   35.13452150021.7490429002009-05-28   33.95794540021.6397010002009-05-29   35.141254850022.02146657002009-06-01   36.16450940022.5267820002009-06-02   36.33430490022.80104734002009-06-03   36.80484540023.1199314002009-06-04   36.79459230022.21178690002009-06-05   36.51441750021.9582143002009-06-08   36.510NaNNaN

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.dataasdata
spy = data.DataReader("SPY","yahoo","1991/1/1")

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