Extending Pandas timeseries

狂风中的少年 提交于 2020-01-03 05:03:08

问题


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.

来源:https://stackoverflow.com/questions/21203437/extending-pandas-timeseries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!