vangorra / python_withings_api

Library for the Withings Health API
MIT License
101 stars 34 forks source link

Pandas df? #51

Closed ethanopp closed 3 years ago

ethanopp commented 3 years ago

Hello!

I'm trying to upgrade from the nokia library to start using this one. I have the auth and everything working so far, just wanted to ask the best way to get data from this library into a pandas df... I've started down this route, but feel like there is probably a better way?

    client = WithingsApi(withings_creds(current_token_dict()), refresh_cb=save_withings_token)

    df = pd.DataFrame(columns=['date_utc', 'weight', 'fat_ratio', 'hydration'])
    meas_result = client.measure_get_meas()
    for x in meas_result.measuregrps:
        date = pd.to_datetime(str(x.date))
        weight = get_measure_value(x, with_measure_type=MeasureType.WEIGHT)
        fat_ratio = get_measure_value(x, with_measure_type=MeasureType.FAT_RATIO)
        hydration = get_measure_value(x, with_measure_type=MeasureType.HYDRATION)

        if weight and fat_ratio:
            df = df.append({'date_utc': date, 'weight': weight, 'fat_ratio': fat_ratio, 'hydration': hydration},
                           ignore_index=True)

    df = df.set_index(df['date_utc'].apply(lambda x: x.replace(tzinfo=None)))
    df = df[['weight', 'fat_ratio', 'hydration']]
    # Convert to lbs
    df['weight'] *= 2.20462

Thanks!

vangorra commented 3 years ago

I'm not familiar with pandas however this looks like a reasonable way to gather weight and fat ration data for a specific time frame.

ethanopp commented 3 years ago

Alright no worries will keep as is for now since its working... thanks!