From d043784cb816f359c28414118fb7a8b2aa76f10d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 26 Sep 2014 22:57:36 -0400 Subject: [PATCH] BUG : deal with empty list passed to boxplot If a data list is empty, return a dict full of np.nan. Closes #3569 and addresses part of https://github.com/pydata/pandas/issues/8382 --- lib/matplotlib/cbook.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index ee026c007739..484cf2fc2ea8 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -1896,7 +1896,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None): ======== =================================== label tick label for the boxplot mean arithemetic mean value - median 50th percentile + med 50th percentile q1 first quartile (25th percentile) q3 third quartile (75th percentile) cilo lower notch around the median @@ -1962,12 +1962,31 @@ def _compute_conf_interval(data, med, iqr, bootstrap): input_whis = whis for ii, (x, label) in enumerate(zip(X, labels), start=0): + # restore whis to the input values in case it got changed in the loop + whis = input_whis + # empty dict stats = {} - stats['label'] = label - # restore whis to the input values in case it got changed in the loop - whis = input_whis + # note tricksyness, append up here and then mutate below + bxpstats.append(stats) + + # if empty, bail + if len(x) == 0: + stats['fliers'] = np.array([]) + stats['mean'] = np.nan + stats['med'] = np.nan + stats['q1'] = np.nan + stats['q3'] = np.nan + stats['cilo'] = np.nan + stats['ciho'] = np.nan + stats['whislo'] = np.nan + stats['whishi'] = np.nan + stats['med'] = np.nan + continue + + # up-convert to an array, just to be safe + x = np.asarray(x) # arithmetic mean stats['mean'] = np.mean(x) @@ -2021,9 +2040,9 @@ def _compute_conf_interval(data, med, iqr, bootstrap): np.compress(x > stats['whishi'], x) ]) - # add in teh remaining stats and append to final output + # add in the remaining stats stats['q1'], stats['med'], stats['q3'] = q1, med, q3 - bxpstats.append(stats) + return bxpstats