Guided Project: Clean And Analyze Employee Exit Surveys: KeyError: 0

Hello! I’m doing the Guided Project: Clean And Analyze Employee Exit Surveys. I’m trying to plot a matplotlib boxplot a get a KeyError: 0

fig, ax = plt.subplots()
ax.boxplot(dete_resignations["role_start_date"].dropna())
ax.set_ylim(1960, 2020)

And I get the following error:

KeyErrorTraceback (most recent call last)
<ipython-input-32-c7675ce88d2d> in <module>()
      1 fig, ax = plt.subplots()
----> 2 ax.boxplot(dete_resignations["cease_date"])

/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

On my local PC everything works fine. What can cause the problem?

hey @artur.sannikov96

The code you have mentioned in the post and the error line indicated show different columns from the same dataset.

first code:

fig, ax = plt.subplots()
ax.boxplot(dete_resignations["role_start_date"].dropna())
ax.set_ylim(1960, 2020)

code in error:

fig, ax = plt.subplots()
ax.boxplot(dete_resignations["cease_date"])

I tried both the codes; the second code works too if we add dropna() method to it similar to the first one.

Please share your notebook, in case this still doesn’t resolve your issue, as I can’t completely understand what exactly you are trying to do.

please follow the guidelines to share the notebook:

Here is my notebook. I have already resolved the problem by using seaborn plots, but you can find the same error at the end of the notebook.
Basics.ipynb (278.0 KB)

Click here to view the jupyter notebook file in a new tab