Search code examples
python-3.xpandaskeyerror

How to ignore a key error and continue the while loop


I am trying to create a dataframe with python's pandas library utilizing data obtained with a requests response. The problem is when there is not that item available on the API so it raises a KeyError and crashes the program.

The source data frame is being iterated over each product name. It then takes the product name of that row and finds how many different SKUs exists, creating a row in a new dataframe for each SKU and adding some quantities and other needed information to the new dataframe. The idea is to have a row with ALL the same information on the first dataframe repeated however many SKUs there are updated with the quantity and package ID for that SKU.

If the length of the response returned is 0, I still want it to append the row from the first data frame

def create_additional_rows_needed(comb_data):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test")

new_combined_data = pd.DataFrame(columns=comb_data.columns)

COVA_DATA_LEN = 2993

row = 0
current_item = ''
   
while row < len(comb_data):
    number_of_skus = 0
    current_item = comb_data.iloc[row, 1]
    if (len(current_item)) is not None:
        number_of_skus = len(find_gb_product(current_item))
    else:
        number_of_skus = 0
    current_quantity = find_gb_product(current_item).iloc[number_of_skus - 1, find_gb_product(current_item).columns.get_loc('quantity')]
    logger.info('Current Quantity: {}'.format(current_quantity))
    current_package = find_gb_product(current_item)['lot_number'][number_of_skus - 1]

    if number_of_skus == 0:
        pass

    while number_of_skus > 0:
        

        logger.info('Current Item: {}'.format(current_item))
        logger.info('Number of Skus: {}'.format(number_of_skus))
        logger.info('Appending: {}'.format(comb_data.iloc[row, 1]))
        

        new_combined_data = new_combined_data.append([comb_data.iloc[row, :]])
        new_combined_data.iloc[-1, new_combined_data.columns.get_loc('TotalOnHand')] = current_quantity
        new_combined_data.iloc[-1, new_combined_data.columns.get_loc('PackageId')] = current_package
                
        number_of_skus = number_of_skus - 1

    logger.info('Finished index {}'.format(row))
    row = row + 1
    logger.info('Moving to index {}'.format(row))

return new_combined_data

It goes well for every item with the exception of a few. Here is the error I get.

KeyError  
   2889                 return self._engine.get_loc(casted_key)
   2890             except KeyError as err:
-> 2891                 raise KeyError(key) from err
   2892 
   2893         if tolerance is not None:

KeyError: 'quantity'

This has taken up my entire weekend and all my sleep and is due Monday Morning at 10am MST with only two days notice. Please help me.


Solution

  • Catching the error and continuing should work. Something along the lines of:

    while row < len(comb_data):
        ....
        try:
            current_quantity = find_gb_product(current_item).iloc[number_of_skus - 1, find_gb_product(current_item).columns.get_loc('quantity')]
        except KeyError:
            continue
        ....