在Python中使用darkskylib库时出现错误请求错误

时间:2019-11-16 13:25:58

标签: python http-error darksky

我得到一个

  

HTTPError:错误的响应

当尝试使用Python中的darkskylib接收Dark Sky API的天气数据时。实际上,这是一个400错误的请求代码。

似乎只有当我在熊猫数据框实例中使用循环时才会发生,因为当我为单个实例运行代码时,我将获得正确的值以及在浏览器中使用直接URL请求时。< / p>

这是我稍后要调用的函数(df是数据框)

def engineer_features(df):
    from datetime import datetime as dt
    from darksky import forecast

    print("Add weather data...")

    # Add Windspeed
    df['ISSTORM'] = 0

    # Add Temperature
    df['ISHOT'] = 0
    df['ISCOLD'] = 0

    # Add Precipitation probability 
    #    (because the weather station is not at the coordinate of the taxi 
    #    only a probability is added, but in regard to the center of Porto 
    #    (because else the API calls would have been costly))
    df['PRECIPPROB'] = 0

    # sort data frame
    data_times = df.sort_values(by='TIMESTAMP')

    # initialize variable for previous day's date day (day before the first day)
    prevDay = data_times.at[0,'TIMESTAMP'].date().day - 1

    #initialize hour counter
    hourCount = 0

    # add personal DarkSky API key and assign with location data
    key = 'MY_API_KEY'
    PORTO = key, 41.1579, -8.6291

    # loop through the sorted dataframe and add weather related data to the main dataframe
    for index, row in data_times.iterrows():

        # if the new row is a new day make a new API call for weather data of that new day
        if row["TIMESTAMP"].day != prevDay:

            # get Weather data
            t = row["TIMESTAMP"].date().isoformat()
            porto = forecast(*PORTO, time=t)
            porto.refresh(units='si')

        ###...more code

1 个答案:

答案 0 :(得分:0)

我的特殊问题是我将datetime转换为date。所以不用写

t = row["TIMESTAMP"].date().isoformat()

我需要写

t = row["TIMESTAMP"].isoformat()