Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't handle for forecastio.utils.PropertyUnavailable keyError #65

Open
jm20122012 opened this issue Aug 9, 2019 · 2 comments
Open

Comments

@jm20122012
Copy link

jm20122012 commented Aug 9, 2019

I've got a script built to report daily data forecasts, but sometimes I get the forecastio.utils.PropertyUnavailable error. I understand that this means there is no data for the particular forecast item I'm looking for. What I'm asking is how do I handle for these errors? This error is causing my script not to finish retrieving the forecast because of this error.

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.6/site-packages/forecastio/models.py", line 103, in __getattr__
    return self.d[name]
KeyError: 'precipType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "DarkSky.py", line 100, in <module>
    main()
  File "DarkSky.py", line 97, in main
    getDaily(forecast_data)
  File "DarkSky.py", line 70, in getDaily
    'Precip_Type' : i.precipType,
  File "/home/user/.local/lib/python3.6/site-packages/forecastio/models.py", line 107, in __getattr__
    " or is not available for this forecast".format(name)
forecastio.utils.PropertyUnavailable: Property 'precipType' is not valid or is not available for this forecast

I'd be fine with being able to say something like:
'Precipitation Type: none or no precipitation type data exists for this forecast'

The problem is I can't figure out how to handle for this error.

I've tried


exception KeyError:
    print('No data exists for this forecast variable')

and

exception forecastio.utils.PropertyUnavailable:
    print('No data exists for this forecast variable')

But neither of these work. I noticed in the traceback that this ultimately results from a KeyError exception, but using this as an Exception raise doesn't work.

Can anyone help with how to handle for these errors? Thanks

@sjmccorm1993
Copy link

@Tesla-Nikola-2012 This example is for hourly data, but the same principle applies:

byHour = forecast.hourly()
series_list = []

    # List of all possible measurements of interest returned by Dark
    # Sky forecast, along with corresponding desired output name
    measurements = {
        "summary": "summary",
        "temperature": "temperature",
        "precipIntensity": "precip_intensity",
        "precipProbability": "precip_prob",
        "humidity": "humidity",
        "windSpeed": "wind_speed",
        "windGust": "wind_gust",
        "cloudCover": "cloud_cover",
        "uvIndex": "uv_index",
        "visibility": "visibility"
    }

    for data in byHour.data:
        s = pd.Series([])

        for key, value in data.d.items():
            if key in measurements.keys():
                s[measurements[key]] = value

        series_list.append(s)
return pd.DataFrame(series_list)

Then the missing measurements end up as NaN values in the resulting dataframe, and your script doesn't break.

@jm20122012
Copy link
Author

@sjmccorm1993

So I'm not 100% sure what exactly is being done here to handle for the keyError. Are you suggesting using pd.Series() to initially hold the values, and then assign the values to the elements in the dict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants