How do you reference column names from get_pricing?

Hi I am trying to reference the column names for a sample from get pricing:

The code below is just getting the cumulative and creating a counter my issue is with this part: ['Equity(34103 [VTA])'], what would be the correct way of referencing this column

output['counter'] = output.diff().ne(0).cumsum()

df2 = output.groupby('counter')['Equity(34103 [VTA])'].min().to_frame(name='value').join(
output.groupby('counter')['Equity(34103 [VTA])'].count().rename('number'))

1 response

The get_pricing method will return either a pandas series, dataframe, or panel depending upon the number of assets and fields. With a single asset and a single field, it returns a series. With either multiple assets or fields, it will return a dataframe. With both multiple assets and multiple fields , it returns a panel. Referencing specific values is a bit different for each case.

Since the request was how to reference the column ['Equity(34103 [VTA])'], I'll assume the get_pricing method was called with multiple assets and a single data field. Something like this

my_assets = symbols(['VTA', 'IBM', 'AAPL'])
output = get_pricing(my_assets, fields=['price', 'volume'])



In this case,'output' will be a dataframe with an index of dates and three columns, one for each asset. The column labels are the assets. The labels are not strings but asset objects. This causes confusion at times. Therefore, to reference a specific column one simply needs to reference the asset. Something like this to get the column for VTA above

# Both these approaches yield a series with just the VTA column
# First get the VTA object
vta = symbols('VTA')
vta_series = output[vta]
vta_series = output.loc[vta]



These will slice the dataframe and return a series. If one wants to keep the dataframe 'as is' but just have the VTA column, then use the filter method. One can also provide a list of columns if desired.

# This approach keeps the dataframe but just the desired column(s)
vta = symbols('VTA')
vta_series = output.filter([vta])



The key is to use the asset object as the label and not a string. Hope that helps.

5