Skip to content

Commit

Permalink
BUG : deal with empty list passed to boxplot
Browse files Browse the repository at this point in the history
If a data list is empty, return a dict full of np.nan.

Closes matplotlib#3569 and adresses part of pandas-dev/pandas#8382
  • Loading branch information
tacaswell committed Sep 27, 2014
1 parent 236355c commit dde007a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/matplotlib/cbook.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit dde007a

Please sign in to comment.