import bisect


def add_price_tier(row, bins):
    """
    Add the item's price tier to the row
    :param row: A dict containing metadata for one item
    :param bins: A list of bins for assigning price tiers as a Spark broadcast
    variable
    :return: A dict containing the original data plus the item's price tier
    """
    # We don't want any reviews for which the price is either NaN or 0
    # or which don't belong to any category
    if ('price' not in row) or (not row['price']):
        row['price'] = 0.0

    if row['price'] == 0.0:
        tier = 0
    else:
        tier = bisect.bisect_left(bins.value, row['price'])

    # price tier
    row['price_tier'] = tier
    return row