Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
help dealing with missing bars

I'd like to be able to drop all missing bars (for which no trade data exists) for a set of stocks. I've attached be beginnings of an algorithm, using the batch transform to return minutely data:

@batch_transform(refresh_period=R_P, window_length=W_L, clean_nans=False) # set globals R_P & W_L above  
def get_data(datapanel,sids):  
    p = datapanel['price']  
    v = datapanel['volume']  
    return [p,v]  

I want to effectively create price and volume vectors for each security, with no NaNs in the vectors. The vectors could be of unequal length.

Any ideas on how to best handle this? I could write a bunch of code to manage it, but perhaps there is an elegant solution with pandas.

Grant

1 response

Here's what I came up with (see attached). The core of it is:

    prices_dict = {}  
    volumes_dict = {}  
    for stock in context.stocks:  
        prices_dict[stock] = prices[stock].dropna().values  
        volumes_dict[stock] = volumes[stock].dropna().values  

I haven't checked for NaNs yet, but presumably .dropna() does the trick. Python to the rescue!

If you have a better approach, please share it.

Grant