0

I am getting a

HTTPError: Bad response

when trying to receive weather data of the Dark Sky API using the darkskylib in Python. Actually it is a 400 bad request code.

It seems it only happens when I use a loop through my pandas dataframe instances because when I run my code for a single instance I am getting the correct values as well as when I am using a direct URL request in my browser.

Here is my function which is called later (with df being the dataframe)

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
JoeBe
  • 1,224
  • 3
  • 13
  • 28

1 Answers1

0

My particular issue was that I converted my datetime into date. So instead of writing

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

I need to write

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

JoeBe
  • 1,224
  • 3
  • 13
  • 28