Boxplot--keyerror:0

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: https://app.dataquest.io/m/348/guided-project%3A-clean-and-analyze-employee-exit-surveys/6/create-a-new-column

My Code:

import matplotlib.pyplot as plt
%matplotlib inline
fig=plt.figure()
ax1=fig.add_subplot(1,2,1)
ax2=fig.add_subplot(1,2,2)
ax1.boxplot(dete_resignations['cease_date'])
ax2.boxplot(dete_resignations['dete_start_dete'])
plt.show()

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'])
      8 plt.show()

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/__init__.py 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/_axes.py 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/cbook.py in boxplot_stats(X, whis, bootstrap, labels)
   1978 
   1979     # convert X to a list of lists
-> 1980     X = _reshape_2D(X)
   1981 
   1982     ncols = len(X)

/dataquest/system/env/python3/lib/python3.4/site-packages/matplotlib/cbook.py 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/series.py in __getitem__(self, key)
    621         key = com._apply_if_callable(key, self)
    622         try:
--> 623             result = self.index.get_value(self, key)
    624 
    625             if not is_scalar(result):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py 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
imagen

Or you can use seaborn instead of pyplot


imagen

Hope it works, good luck!