Search code examples
pythondarksky

Darksky Loop through one year of daily data - Datetime Error


I attempt to loop through daily weather data in 2019 using forecastiopy but the error keeps showing. Not sure what the problem is.

import pandas as pd 
import requests
import json 
from forecastiopy import *
from datetime import date, timedelta, datetime
import datetime

key = 'xxxxx'

city = [40.730610, -73.935242]
start = datetime.datetime(2019, 1, 1)


for day in range(1,365):
    fio = ForecastIO.ForecastIO(key,
                        units=ForecastIO.ForecastIO.UNITS_SI,
                        lang=ForecastIO.ForecastIO.LANG_ENGLISH,
                        latitude=city[0], 
                        longitude=city[1],
                        time=start+datetime.timedelta(day))
    daily = FIODaily.FIODaily(fio)
    print ('Max Temperature', daily.get_day(day)['temperatureMax'])
    print ('Min Temperature:', daily.get_day(day)['temperatureMin'])
    print ('Precipitation Pobability:', daily.get_day(day)['precipProbability'])
    print ('Precipitation Intensity', daily.get_day(day)['precipIntensity'])

The error shown is below.

enter image description here


Solution

  • ForecastIO.ForecastIO(key,
        ...,
        time=start+datetime.timedelta(day))
    

    Here, the time argument is supposed to be a string that is directly mapped to the Dark Sky API:

    time

    Either be a UNIX time (that is, seconds since midnight GMT on 1 Jan 1970) or a string formatted as follows: [YYYY]-[MM]-[DD]T[HH]:[MM]:[SS][timezone]. [...]

    Therefore, you could format the datetime object using isoformat()

    ForecastIO.ForecastIO(key,
        ...,
        time=(start+datetime.timedelta(day)).isoformat())