I tried many times and different codes to create the boxplot but it does not work. I think my code is correct. but I still have errors showing. Please help!! Thank you!!

Screen Link:

My Code:

import matplotlib.pyplot as plt
%matplotlib inline

What actually happened:

KeyErrorTraceback (most recent call last)
<ipython-input-18-630b7f32599c> in <module>()
      4 ax1=fig.add_subplot(1,2,1)
      5 ax2=fig.add_subplot(1,2,2)
----> 6 ax1.boxplot(dete_resignations['cease_date'])
      7 ax2.boxplot(dete_resignations['dete_start_dete'])

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/ in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/axes/ in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks)
   3210             bootstrap = rcParams['boxplot.bootstrap']
   3211         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3212                                        labels=labels)
   3213         if notch is None:
   3214             notch = rcParams['boxplot.notch']

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/ in boxplot_stats(X, whis, bootstrap, labels)
   1979     # convert X to a list of lists
-> 1980     X = _reshape_2D(X)
   1982     ncols = len(X)

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/ in _reshape_2D(X)
   2226         # one item
   2227         if len(X.shape) == 1:
-> 2228             if hasattr(X[0], 'shape'):
   2229                 X = list(X)
   2230             else:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in __getitem__(self, key)
    621         key = com._apply_if_callable(key, self)
    622         try:
--> 623             result = self.index.get_value(self, key)
    625             if not is_scalar(result):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/ in get_value(self, series, key)
   2558         try:
   2559             return self._engine.get_value(s, k,
-> 2560                                           tz=getattr(series.dtype, 'tz', None))
   2561         except KeyError as e1:
   2562             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

Hi @candiceliu93

That’s a weird error, but first i’d like to know why do you want to plot a boxplot using columns that have dates? Then, are those columns clear? If i remember correctly some of the values on those columns have different formats like 2012 and 01/2014

I think that may be the issue, because the code is correct

Hi @alegiraldo666

I took the year and changed data type to float for that column. I want to see what is the outliner of this column.

@candiceliu93 I see, for some reason my notebook is a mess in that project. But i tried to do that box plot and got the same error. However if i use this instead dete_resignations.dete_start_date.plot(kind='box') the plot is generated

The other way (that i just remember) is using the method df.boxplot() The plot isn’t as beautiful as a pyplot plot, but it’s still decent enough

Or you can use seaborn instead of pyplot


Hope it works, good luck!