Pandas Guided Project - Step 3. Initial Exploration Cleaning - jupyter question

Hello. Running into an attribute error which has left me with some curiosity but, yet some confusion.

Instructions:
You likely found that the price and odometer columns are numeric values stored as text. For each column:

  • Remove any non-numeric characters.
  • Convert the column to a numeric dtype.
autos['odometer_km'] = autos['odometer_km'].str.replace(',', '').str.split('k')
autos['odometer_km'] = [x[0] for x in autos['odometer_km']]
autos['odometer_km'] = autos['odometer_km'].astype('int64')
print(autos['odometer_km'])

When I do ‘Run All Cells’ the program executes just fine.

However if I run the cell, alone, I get this error below. Why?
" AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas"

Thanks!! :slight_smile:


Full Error Msg below.



AttributeErrorTraceback (most recent call last)
<ipython-input-43-1cbd26c74286> in <module>()
----> 1 autos['odometer_km'] = autos['odometer_km'].str.replace(',', '').str.split('k')
      2 autos['odometer_km'] = [x[0] for x in autos['odometer_km']]
      3 autos['odometer_km'] = autos['odometer_km'].astype('int64')
      4 print(autos['odometer_km'])

/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

When u ran all the cells including mentioned one, odometer_km column was converted to dtype ‘int’. Running it again will cause error as .str accessor won’t work on column containing dtype, int.

1 Like

Thanks! So I didn’t do anything wrong? :smiley:

1 Like