Clean And Analyze Employee Exit Surveys Step 5 (string values)

Screen Link:

My Code:

dete_resignations['cease_date'] = dete_resignations['cease_date'].str.split('/').str.get(-1).astype('float')
dete_resignations['cease_date'].sort_index(ascending=True)

What I expected to happen:
An list of years in ascending order

What actually happened:


AttributeErrorTraceback (most recent call last)
<ipython-input-58-76a6fc5ad2b4> in <module>()
----> 1 dete_resignations['cease_date'] = dete_resignations['cease_date'].str.split('/').str.get(-1).astype('float')
      2 dete_resignations['cease_date'].sort_index(ascending=True)
      3 
      4 tafe_resignations['cease_date'] = tafe_resignations['cease_date'].astype('float')
      5 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in __getattr__(self, name)
   3608         if (name in self._internal_names_set or name in self._metadata or
   3609                 name in self._accessors):
-> 3610             return object.__getattribute__(self, name)
   3611         else:
   3612             if name in self._info_axis:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/accessor.py in __get__(self, instance, owner)
     52             # this ensures that Series.str.<method> is well defined
     53             return self.accessor_cls
---> 54         return self.construct_accessor(instance)
     55 
     56     def __set__(self, instance, value):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/strings.py in _make_accessor(cls, data)
   1908             # (instead of test for object dtype), but that isn't practical for
   1909             # performance reasons until we have a str dtype (GH 9343)
-> 1910             raise AttributeError("Can only use .str accessor with string "
   1911                                  "values, which use np.object_ dtype in "
   1912                                  "pandas")

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

From my understanding this error should not occur because the .astype(float) function is at the end so the .str function is processing the string value before it is converted to a float value. But I still get this error.

I tested out your code, and it ran fine.

The only possible reasons for this that I can think of -

  • You have a particular value for dete_resignations['cease_date'] that is not a string somewhere.
  • You ran your code cell more than once

It’s most likely the 2nd one, because I ran your code, it ran fine. And when I ran the same code again, it threw me that same error.

If you have already converted that particular column into the floats, then you can’t run that same code again. Because it no longer has any strings stored in it.