Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Two lists after resampling, how can I make them of equal length?

I'm getting price history and volume history for a given sid and then re-sampling the data to x min time frames. I need to have both lists the same length for another function that uses them or that function will fail. How can I take the two lists and make them both the same length as the shorter of the two lists? The code I have thus far is posted below. As you can see I've added a function to drop the NaN values but I'm also thinking this should be done at the end maybe to avoid putting the sequence of price/volume out of sync? Keep in mind I'm basically only a couple weeks into python and quantopian so it's probably a little ugly :) Thanks for help again, it is very much appreciated and I wouldn't have had a chance at comming this far if it weren't for the community.

Get price history and resample

recent_prices = data.history(stock, 'price', context.history_range, '1m') ##.values
recent_volumes = data.history(stock, 'volume', context.history_range, '1m') ##.values

recent_prices_sampled = recent_prices.resample(str(context.candlePeriod) + 'T', label='right').mean()
recent_volumes_sampled = recent_volumes.resample(str(context.candlePeriod) + 'T', label='right').mean()

recent_prices_sampled = recent_prices_sampled.dropna()
recent_volumes_sampled = recent_prices_sampled.dropna()

Make the lists equal length for the ML function

recent_prices_sampled_new = []
recent_volumes_sampled_new = []

if len(recent_prices_sampled) > len(recent_volumes_sampled):
for i in xrange(len(recent_volumes_sampled)):
recent_volumes_sampled_new.append([recent_volumes_sampled[i][0]])
else:
for i in xrange(len(recent_prices_sampled)):
recent_prices_sampled_new.append([recent_prices_sampled[i][0]])

Get the changes

price_changes = np.diff(recent_prices_sampled_new).tolist()
volume_changes = np.diff(recent_volumes_sampled_new).tolist()