在Python中使用darkskylib库时遇到错误的请求错误

问题描述 投票:0回答:1

我得到一个

HTTPError:错误的响应

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

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

这是我的函数,稍后会调用(以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
python http-error darksky
1个回答
0
投票

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

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

我需要写

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

© www.soinside.com 2019 - 2024. All rights reserved.