glassnode / glassnode-api-python-client

The official Python client library for Glassnode's API – https://docs.glassnode.com
45 stars 18 forks source link

requesting API with multiple columns #5

Open TurtleJelly opened 3 years ago

TurtleJelly commented 3 years ago

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)
neilsummers commented 3 years ago

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)