Back to Community
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.

Loading notebook preview...

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.