Search code examples
pandasjupyter-notebookdata-science-experiencewatson-studio

How do I load data from a StreamingBody object using Insert to Code to pandas in Watson Studio?


The Insert to Code feature enables you to access data stored in Cloud Object Storage when working in Jupyter notebooks in Watson Studio. Some file types (e.g. txt files) will have just StreamingBody and Credentials as insert to code options:

Screenshot of Insert to Code functionality

How can I use the StreamingBody object to access my data?


Solution

  • These code snippets do not always get you the whole way to access your data. For the Insert StreamingBody object option, you can load the data into a pandas dataframe using io.BytesIO, for example:

    import io
    import pandas as pd
    
    df = pd.read_table(io.BytesIO(streaming_body_1.read()))
    

    where streaming_body_1 is a boto3 client get_object()['Body']