Get pandas dataframes from lists of alpaca objects

Is there a utility function, or some other way to get pandas dataframes from account.list_…()? For example, api.get_barset() has the .df attribute but account.list_positions() does not. Is what converts alpaca objects to dataframes an internal alpaca function that I can import and run on the output of account.list_positions(), .list_orders(), etc? Any insight is appreciated. Thanks!

In case someone else searches for this, I realized that a straightforward way to do this is:

positions = api.list_positions()
positions = pd.DataFrame({
     'ticker': [x.symbol for x in positions],
     'qty': [x.qty for x in positions], etc

Still, I wonder if there’s a built-in function somewhere that yields the same result.

1 Like

While not a built-in function. I saw a solution from nad that I’ve found very useful. You flatten the object by calling vars(object) on it first. Then handle the indexing as you roll up the dataframe:

do you mean…

positions = api.list_positions()
positions = pd.DataFrame({'ticker': [1.symbol], 'qty': [2.qty],.....})

sry i know this is probably a silly question

lol that looks way easier but yet i’ve tried it and still can’t get it working in colab. ugh i’m a noob.

try pd.DataFrame([position._raw for position in positions])