【问题标题】:Getting Bad Request error when using darkskylib library in Python在 Python 中使用 darkskylib 库时出现错误请求错误
【发布时间】:2019-11-16 13:25:58
【问题描述】:

我得到了一个

HTTPError: 错误响应

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

似乎只有当我在我的 pandas 数据框实例中使用循环时才会发生这种情况,因为当我为单个实例运行我的代码时,我得到了正确的值,以及当我在浏览器中使用直接 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】:

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

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

    我要写

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

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多