def get_class_impurity(class_values):

    """Calculate the Gini index of impurity for the passed class values"""

    # get the frequency of each class
    class_frequency_dict = class_values.value_counts(normalize=True).to_dict()

    # penalize low class frequencies with the square, so that purity decreases
    purity = 0
    for class_value, frequency in class_frequency_dict.items():
        purity += frequency ** 2

    # impurity is the complementary of purity
    return 1 - purity