Detrous / darksky

Python API wrapper for the DarkSky (async&sync)
https://pypi.org/project/darksky_weather/
MIT License
81 stars 22 forks source link

Add a to_frame() method to forecast class #51

Open dzimmanck opened 4 years ago

dzimmanck commented 4 years ago

Is your feature request related to a problem? Please describe. Many libraries that expect weather data as an input use pandas dataframes as the standard data object (PVLib for example). Also, being able to return a pandas dataframe will make it easier to print, plot, and process data returned from darksky.

Describe the solution you'd like A method called "to_frame()" which returns a DatetimeIndex'd dataframe of weather data.

weather = darksky.get_time_machine_forecast(...) hourly_forecast_dataframe = weather.hourly.to_frame()

# print the start of the forecast print(hourly_forecast_dataframe.head())

# plot the temperature hourly_forecast_dataframe.plot(y='temperature')

Describe alternatives you've considered Currently I use a function which accepts a forecast as an argument and returns a dataframe. This works just fine, but a built-in method would be nice."

dzimmanck commented 4 years ago

For reference, here is the function I use.

import pandas as pd

def darksky_forecast_to_dataframe(forecast):
    """Convert darksky forecast data list to a dataframe

    Keyword arguments:
    forecast -- list of daksky forecast items

    Returns:
    df -- DatetimeIndexed pandas dataframe of the forecast data
    """

    # convert list of forecast items to list of dictionaries
    data = [entry.__dict__ for entry in forecast]

    # make the dataframe
    df = pd.DataFrame(data)

    # use time as the index
    df.set_index('time', inplace=True)

    return df
Detrous commented 4 years ago

Hi @dzimmanck Pandas are a heavy requirements. I don’t think it will be needed for just one small function. I will think what can I do