Skip to content

requesting API with multiple columns #5

Open

Description

@TurtleJelly

Hi Team Glassnode,

When retrieving API with multiple columns, the column name is 'o' not 'v'.

(e.g : https://api.glassnode.com/v1/metrics/supply/hodl_waves)

And it cannot be retrieved properly with current API connector.

I have added below code to fix the problem, but the iterator make the whole process quite slow...

try:
    df = pd.DataFrame(json.loads(r.text))
    df = df.set_index('t')
    df.index = pd.to_datetime(df.index, unit='s')
    df = df.sort_index()
    s = pd.DataFrame()
    for r, c in df.iterrows():
        for k, v in c['o'].items():
            s.loc[r, k] = v
    return s

except Exception as e:
    print(e)

Activity

  1. neilsummers commented on Jul 1, 2021

    @neilsummers

    Here is the modified version I am using to allow pulling data with multiple columns.
    This does not iterate over rows so should be faster.
    Note I have also changed the column names to only include metric and not indicator.metric.

            try:
                df = pd.DataFrame(json.loads(r.text))
                df = df.set_index('t')
                df.index = pd.to_datetime(df.index, unit='s')
                df = df.sort_index()
                if 'v' in df:
                    s = df.v
                    # s.name = '_'.join(url.split('/')[-2:])
                    s.name = url.split('/')[-1]
                    return pd.DataFrame(s)
                else:  # 'o' contains json object of multiple columns
                    index = df.index
                    df = pd.json_normalize(df.o)
                    df.index = index
                    col_name_prefix = url.split('/')[-1]
                    df.columns = [f'{col_name_prefix}.{col}' for col in df.columns]
                    return df
            except Exception as e:
                print(e)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions