Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d043784

Browse files
committed
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 pandas-dev/pandas#8382
1 parent 236355c commit d043784

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lib/matplotlib/cbook.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None):
18961896
======== ===================================
18971897
label tick label for the boxplot
18981898
mean arithemetic mean value
1899-
median 50th percentile
1899+
med 50th percentile
19001900
q1 first quartile (25th percentile)
19011901
q3 third quartile (75th percentile)
19021902
cilo lower notch around the median
@@ -1962,12 +1962,31 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
19621962

19631963
input_whis = whis
19641964
for ii, (x, label) in enumerate(zip(X, labels), start=0):
1965+
# restore whis to the input values in case it got changed in the loop
1966+
whis = input_whis
1967+
19651968
# empty dict
19661969
stats = {}
1967-
stats['label'] = label
19681970

1969-
# restore whis to the input values in case it got changed in the loop
1970-
whis = input_whis
1971+
# note tricksyness, append up here and then mutate below
1972+
bxpstats.append(stats)
1973+
1974+
# if empty, bail
1975+
if len(x) == 0:
1976+
stats['fliers'] = np.array([])
1977+
stats['mean'] = np.nan
1978+
stats['med'] = np.nan
1979+
stats['q1'] = np.nan
1980+
stats['q3'] = np.nan
1981+
stats['cilo'] = np.nan
1982+
stats['ciho'] = np.nan
1983+
stats['whislo'] = np.nan
1984+
stats['whishi'] = np.nan
1985+
stats['med'] = np.nan
1986+
continue
1987+
1988+
# up-convert to an array, just to be safe
1989+
x = np.asarray(x)
19711990

19721991
# arithmetic mean
19731992
stats['mean'] = np.mean(x)
@@ -2021,9 +2040,9 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
20212040
np.compress(x > stats['whishi'], x)
20222041
])
20232042

2024-
# add in teh remaining stats and append to final output
2043+
# add in the remaining stats
20252044
stats['q1'], stats['med'], stats['q3'] = q1, med, q3
2026-
bxpstats.append(stats)
2045+
20272046

20282047
return bxpstats
20292048

0 commit comments

Comments
 (0)