darkskylib icon indicating copy to clipboard operation
darkskylib copied to clipboard

HTTPError: Bad response when making requests in loop

Open JoBerkner opened this issue 6 years ago • 1 comments

I am getting a

HTTPError: Bad response

I was so free to actually get the correct error code. 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 asssign with location data
    key = 'MYKEY'
    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

JoBerkner avatar Nov 15 '19 07:11 JoBerkner

How many of these requests are you making? Keep in mind that your API key will allow you to do max 1000 requests per day.

lukaskubis avatar Dec 26 '19 12:12 lukaskubis