Search code examples
pythondictionarydataframedata-conversionbloomberg

How to convert a dictionary to a Dataframe


I'm trying to convert a dictionary output into a Dataframe.

For my specific project, I'm using the Bloomberg service API to request a handful of historical datapoints for a stock ticker. They give me the output in dictionary form, but I need to convert it into a more manageable DataFrame. So far all the solutions appear pretty complex. Is there a straightforward pythonic way of accomplishing this?

Thanks!

The snippet of code producing the output:

def main():
    output = {}
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print ("Connecting to %s:%s" % (options.host, options.port))
    # Create a Session
    session = blpapi.Session(sessionOptions)

    # Start a Session
    if not session.start():
        print ("Failed to start session.")
        return

    try:
        # Open service to get historical data from
        if not session.openService("//blp/refdata"):
            print ("Failed to open //blp/refdata")
            return

        # Obtain previously opened service
        refDataService = session.getService("//blp/refdata")

        # Create and fill the request for the historical data
        request = refDataService.createRequest("HistoricalDataRequest")
        request.getElement("fields").appendValue("BEST_SALES")
        request.getElement("fields").appendValue("BEST_EBITDA")
        request.getElement("fields").appendValue("BEST_EPS")
        request.getElement("fields").appendValue("CURR_ENTP_VAL")
        request.getElement("fields").appendValue("CUR_MKT_CAP")
        request.getElement("fields").appendValue("LAST_PRICE")
        # Elements passed to it
        request.getElement("securities").appendValue("MSFT US Equity")

        # Add overrides
        overrides = request.getElement("overrides")
        override1 = overrides.appendElement()
        override1.setElement("fieldId", "BEST_FPERIOD_OVERRIDE")
        override1.setElement("value", "1FY")
        override2 = overrides.appendElement()
        override2.setElement("fieldId", "BEST_CONSOLIDATED_OVERRIDE")
        override2.setElement("value", "C")
        override3 = overrides.appendElement()
        override3.setElement("fieldId", "EQY_FUND_CRNCY")
        override3.setElement("value", "USD")
        # Add historical adjustments
        request.set("periodicityAdjustment", "ACTUAL")
        request.set("periodicitySelection", "DAILY")
        request.set("maxDataPoints", 100)
        ## NEED TO PASS THIS
        request.set("startDate", "20160106")
        request.set("endDate", "20160107")


        print ("Sending Request:", request)
        # Send the request
        session.sendRequest(request)

        # Process received events
        while(True):
            # We provide timeout to give the chance for Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                print (msg)
            if ev.eventType() == blpapi.Event.RESPONSE:
                # Response completely received, so we could exit
                break
    finally:
        # Stop the session
        session.stop()

Output in dictionary form:

HistoricalDataResponse = {
    securityData = {
        security = "MSFT US Equity"
        eidData[] = {
        }
        sequenceNumber = 0
        fieldExceptions[] = {
        }
        fieldData[] = {
            fieldData = {
                date = 2016-01-06
                BEST_SALES = 98338.750000
                BEST_EPS = 3.108000
                CURR_ENTP_VAL = 373535.702300
                CUR_MKT_CAP = 431746.702300
                LAST_PRICE = 54.050000
            }
            fieldData = {
                date = 2016-01-07
                BEST_SALES = 98351.040000
                BEST_EBITDA = 37885.200000
                BEST_EPS = 3.110000
                CURR_ENTP_VAL = 358518.425700
                CUR_MKT_CAP = 416729.425700
                LAST_PRICE = 52.170000
            }
        }
    }
}

Solution

  • You could take a look at the pdblp package (Disclaimer: I'm the author)

    For your example something like should work (I don't currently have a bbg connection though so haven't actually tested this)

    import pdblp
    con = pdblp.BCon()
    con.start()
    
    fields = ['BEST_SALES', 'BEST_EBITDA', 'BEST_EPS', 'CURR_ENTP_VAL',
              'CUR_MKT_CAP', 'LAST_PRICE']
    
    
    ovrds = [('BEST_FPERIOD_OVERRIDE', '1FY'), ('BEST_CONSOLIDATED_OVERRIDE', 'C'),
             ('EQY_FUND_CRNCY', 'USD')]
    
    elms = [('periodicityAdjustment', 'ACTUAL'),
            ('periodicitySelection', 'ACTUAL'), ('maxDataPoints', 100)]
    
    
    df = con.bdh('MSFT US Equity', fields, '20150629', '20150630', elms=elms,
                 ovrds=ovrds)