Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
history API - how to order sids?

With the new history API, is there an elegant way to have the output columns in the same order as a list of sids? For example, in the attached algorithm, I have:

context.stocks = [sid(8554),sid(19920),sid(33652)]  

I'd like the dataframe columns to correspond to the list order of 8554, 19920, 33652. As presently coded, the output does not maintain the order:

2013-12-02 PRINT 19920 8554 33652
2013-11-27 21:00:00+00:00 85.22 181.14 80.95
2013-11-29 18:00:00+00:00 85.73 181 80.98
2013-12-02 14:31:00+00:00 85.85 181.17 80.66

Perhaps there is a pandas thingy that will order the columns according to a list?

1 response

Here's one approach that uses sorting, but it is not quite what I'm looking for, since the order of the original list of stocks is not maintained. However, it'll work.

It doesn't seem like the most efficient solution, since the dataframe has to be sorted every call to handle_data.

Other approaches?

By the way, with the history API, how is the dataframe sid ordering determined? Perhaps it should default to ordering by increasing sid number?

Grant