Extending Pandas timeseries

2019-06-14 11:22发布

I'd like to expand a pandas timeseries when updating with new data. Here's how I'm creating the initial Panel, for stock data. Here's the initial empty panel:

def create_blank_data():
    dates = pd.date_range(start=dt.date(2008, 1, 1), end=dt.date.today())
    attrs = ['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
    symbols_ = symbols.read()
    data = np.zeros((len(symbols_), len(dates), len(attrs)))
    return pd.Panel(data, items=symbols_, major_axis=dates, minor_axis=attrs)

I can then populate the initial data like this:

def test_fresh_data():
    data = create_blank_data()
    for symbol in symbols.read():
        data[symbol] = api(symbol, start='2010-01-01') # returns a Dataframe.
    return data

This seems to work, but, I'm unable to extend the data. If any of the dataframes I load contain newer data, I'd like to update the panel. Currently, if I update the panel using another "data[symbol] = api(symbol, start='2010-01-01')" line, data with a newer dates than is defined in the panel is ignored. My thought it to explicitly expand the panel's date range first.

I've been using this page as reference.

0条回答
登录 后发表回答