# Buggy code
def nom_insights(data: Dict[str, Any], col: str) -> Dict[str, List[str]]:
    """
    Format the insights for plot(df, Nominal())
    """
    # pylint: disable=line-too-long
    # insight dictionary, with a list associated with each plot
    ins: Dict[str, List[str]] = {
        "stat": [],
        "bar": [],
        "pie": [],
        "cloud": [],
        "wf": [],
        "wl": [],
    }

    ## if cfg.insight.constant_enable:
    if data["nuniq"] == 1:
        ins["stat"].append(f"{col} has a constant value")

    ## if cfg.insight.high_cardinality_enable:
    if data["nuniq"] > 50:  ## cfg.insght.high_cardinality_threshold
        nuniq = data["nuniq"]
        ins["stat"].append(f"{col} has a high cardinality: {nuniq} distinct values")

    ## if cfg.insight.missing_enable:
    pmiss = round((data["nrows"] - data["stats"]["npres"]) / data["nrows"] * 100, 2)
    if pmiss > 1:  ## cfg.insight.missing_threshold
        nmiss = data["nrows"] - data["stats"]["npres"]
        ins["stat"].append(f"{col} has {nmiss} ({pmiss}%) missing values")

    ## if cfg.insight.constant_length_enable:
    if data["stats"]["nuniq"] == data["stats"]["npres"]:
        ins["stat"].append(f"{col} has all distinct values")

    ## if cfg.insight.evenness_enable:
    if data["chisq"][1] > 0.999:  ## cfg.insight.uniform_threshold
        ins["bar"].append(f"{col} is relatively evenly distributed")

    ## if cfg.insight.outstanding_no1_enable
    factor = data["bar"][0] / data["bar"][1] if len(data["bar"]) > 1 else 0
    if factor > 1.5:
        val1, val2 = data["bar"].index[0], data["bar"].index[1]
        ins["bar"].append(
            f"The largest value ({val1}) is over {factor} times larger than the second largest value ({val2})"
        )

    ## if cfg.insight.attribution_enable
    if data["pie"][:2].sum() / data["nrows"] > 0.5:
        vals = ", ".join(data["pie"].index[i] for i in range(2))
        ins["pie"].append(f"The top 2 categories ({vals}) take over 50%")

    ## if cfg.insight.high_word_cardinlaity_enable
    if data["nwords"] > 1000:
        nwords = data["nwords"]
        ins["cloud"].append(f"{col} contains many words: {nwords} words")

    ## if cfg.insight.outstanding_no1_word_enable
    factor = (
        data["word_cnts"][0] / data["word_cnts"][1] if len(data["word_cnts"]) > 1 else 0
    )
    if factor > 1.5:
        val1, val2 = data["word_cnts"].index[0], data["word_cnts"].index[1]
        ins["wf"].append(
            f"The largest value ({val1}) is over {factor} times larger than the second largest value ({val2})"
        )

    ## if cfg.insight.constant_word_length_enable
    if data["len_stats"]["Minimum"] == data["len_stats"]["Maximum"]:
        ins["wf"].append(f"{col} has words of constant length")

    return ins


# Traceback (IndexError)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-463fb2fdfb17> in <module>
----> 1 create_report(df)

~/projects/dataprep/dataprep/eda/create_report/__init__.py in create_report(df, title, mode, progress)
50         "resources": INLINE.render(),
51         "title": title,
---> 52         "components": format_report(df, mode, progress),
53     }
54     template_base = ENV_LOADER.get_template("base.html")

~/projects/dataprep/dataprep/eda/create_report/formatter.py in format_report(df, mode, progress)
57         df = to_dask(df)
58         if mode == "basic":
---> 59             comps = format_basic(df)
60         # elif mode == "full":
61         #     comps = format_full(df)

~/projects/dataprep/dataprep/eda/create_report/formatter.py in format_basic(df)
109                 col=col, data=data[col], visual_type="categorical_column"
110             )
--> 111             rndrd = render(itmdt, plot_height_lrg=250, plot_width_lrg=280)
112             stats = format_cat_stats(
113                 data[col]["stats"], data[col]["len_stats"], data[col]["letter_stats"]

~/projects/dataprep/dataprep/eda/distribution/render.py in render(itmdt, yscale, tile_size, plot_width_sml, plot_height_sml, plot_width_lrg, plot_height_lrg, plot_width_wide)
2111         )
2112     elif itmdt.visual_type == "categorical_column":
-> 2113         visual_elem = render_cat(itmdt, yscale, plot_width_lrg, plot_height_lrg)
2114     elif itmdt.visual_type == "numerical_column":
2115         visual_elem = render_num(itmdt, yscale, plot_width_lrg, plot_height_lrg)

~/projects/dataprep/dataprep/eda/distribution/render.py in render_cat(itmdt, yscale, plot_width, plot_height)
1674     tabs = Tabs(tabs=tabs)
1675     # insights
-> 1676     nom_insights(data, col)
1677     # TODO return insights
1678     return tabs

~/projects/dataprep/dataprep/eda/distribution/render.py in nom_insights(data, col)
1727     ## if cfg.insight.attribution_enable
1728     if data["pie"][:2].sum() / data["nrows"] > 0.5:
-> 1729         vals = ", ".join(data["pie"].index[i] for i in range(2))
1730         ins["pie"].append(f"The top 2 categories ({vals}) take over 50%")
1731

~/projects/dataprep/dataprep/eda/distribution/render.py in <genexpr>(.0)
1727     ## if cfg.insight.attribution_enable
1728     if data["pie"][:2].sum() / data["nrows"] > 0.5:
-> 1729         vals = ", ".join(data["pie"].index[i] for i in range(2))
1730         ins["pie"].append(f"The top 2 categories ({vals}) take over 50%")
1731

~/projects/dataprep/.venv/lib/python3.7/site-packages/pandas/core/indexes/base.py in __getitem__(self, key)
3928         if is_scalar(key):
3929             key = com.cast_scalar_indexer(key)
-> 3930             return getitem(key)
3931
3932         if isinstance(key, slice):

IndexError: index 1 is out of bounds for axis 0 with size 1

# Fixed code
def nom_insights(data: Dict[str, Any], col: str) -> Dict[str, List[str]]:
    """
    Format the insights for plot(df, Nominal())
    """
    # pylint: disable=line-too-long
    # insight dictionary, with a list associated with each plot
    ins: Dict[str, List[str]] = {
        "stat": [],
        "bar": [],
        "pie": [],
        "cloud": [],
        "wf": [],
        "wl": [],
    }

    ## if cfg.insight.constant_enable:
    if data["nuniq"] == 1:
        ins["stat"].append(f"{col} has a constant value")

    ## if cfg.insight.high_cardinality_enable:
    if data["nuniq"] > 50:  ## cfg.insght.high_cardinality_threshold
        nuniq = data["nuniq"]
        ins["stat"].append(f"{col} has a high cardinality: {nuniq} distinct values")

    ## if cfg.insight.missing_enable:
    pmiss = round((data["nrows"] - data["stats"]["npres"]) / data["nrows"] * 100, 2)
    if pmiss > 1:  ## cfg.insight.missing_threshold
        nmiss = data["nrows"] - data["stats"]["npres"]
        ins["stat"].append(f"{col} has {nmiss} ({pmiss}%) missing values")

    ## if cfg.insight.constant_length_enable:
    if data["stats"]["nuniq"] == data["stats"]["npres"]:
        ins["stat"].append(f"{col} has all distinct values")

    ## if cfg.insight.evenness_enable:
    if data["chisq"][1] > 0.999:  ## cfg.insight.uniform_threshold
        ins["bar"].append(f"{col} is relatively evenly distributed")

    ## if cfg.insight.outstanding_no1_enable
    factor = data["bar"][0] / data["bar"][1] if len(data["bar"]) > 1 else 0
    if factor > 1.5:
        val1, val2 = data["bar"].index[0], data["bar"].index[1]
        ins["bar"].append(
            f"The largest value ({val1}) is over {factor} times larger than the second largest value ({val2})"
        )

    ## if cfg.insight.attribution_enable
    if data["pie"][:2].sum() / data["nrows"] > 0.5 and len(data["pie"]) >= 2:
        vals = ", ".join(data["pie"].index[i] for i in range(2))
        ins["pie"].append(f"The top 2 categories ({vals}) take over 50%")

    ## if cfg.insight.high_word_cardinlaity_enable
    if data["nwords"] > 1000:
        nwords = data["nwords"]
        ins["cloud"].append(f"{col} contains many words: {nwords} words")

    ## if cfg.insight.outstanding_no1_word_enable
    factor = (
        data["word_cnts"][0] / data["word_cnts"][1] if len(data["word_cnts"]) > 1 else 0
    )
    if factor > 1.5:
        val1, val2 = data["word_cnts"].index[0], data["word_cnts"].index[1]
        ins["wf"].append(
            f"The largest value ({val1}) is over {factor} times larger than the second largest value ({val2})"
        )

    ## if cfg.insight.constant_word_length_enable
    if data["len_stats"]["Minimum"] == data["len_stats"]["Maximum"]:
        ins["wf"].append(f"{col} has words of constant length")

    return ins