Checking data types of specific columns

   df['fire_numbers'].str.isdigit()
   df['fire_numbers'].str.isnumeric()

  -If datatype is not object, it returns AttributeError: Can only use .str accessor with string values!

  -To fix this error ->  we need to convert column 'fire_numbers' datatype to object(string)

   df['fire_numbers'].astype(str).str.isdigit()
   df['fire_numbers'].astype(str).str.isnumeric()
In [53]:
import pandas as pd


df = pd.read_csv('amazon_fires.csv', encoding = "ISO-8859-1")


new_column_name = { 'ano' : 'year' ,  'mes': 'month', 'estado': 'state',
                   'numero': 'fire_numbers','encontro': 'date'}
                                                
df.rename(columns= new_column_name , inplace=True)
     
    
new_column_order = [4,1,0,2,3,]

df = df[df.columns[new_column_order]]


df.head()
Out[53]:
date month year state fire_numbers
0 1/1/1998 Janeiro 1998 Acre 0 Fires
1 1/1/1999 Janeiro 1999 Acre 0 Fires
2 1/1/2000 Janeiro 2000 Acre 0 Fires
3 1/1/2001 Janeiro 2001 Acre 0 Fires
4 1/1/2002 Janeiro 2002 Acre 0 Fires

Checking Datatype of fire_numbers

In [31]:
df.dtypes
Out[31]:
date            object
state           object
fire_numbers    object
dtype: object

Checking if fire_numbers is digit

In [32]:
df['fire_numbers'].str.isdigit()
Out[32]:
0       False
1       False
2       False
3       False
4       False
        ...  
6447     True
6448     True
6449     True
6450     True
6451     True
Name: fire_numbers, Length: 6452, dtype: object

Checking if fire_numbers is numeric

In [34]:
df['fire_numbers'].str.isnumeric()
Out[34]:
0       False
1       False
2       False
3       False
4       False
        ...  
6447     True
6448     True
6449     True
6450     True
6451     True
Name: fire_numbers, Length: 6452, dtype: object

Let's change datatype to float to see the error

- Fist we need to remove text "Fires"
- Then we can convert it to float
In [54]:
df['fire_numbers'] = df['fire_numbers'].str.strip("Fires")

df['fire_numbers'] = df['fire_numbers'].astype(float)

Checking if fire_numbers is digit

In [49]:
df['fire_numbers'].str.isdigit()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-49-c3046e6c8d32> in <module>
----> 1 df['fire_numbers'].str.isdigit()

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5268             or name in self._accessors
   5269         ):
-> 5270             return object.__getattribute__(self, name)
   5271         else:
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):

~/anaconda3/lib/python3.7/site-packages/pandas/core/accessor.py in __get__(self, obj, cls)
    185             # we're accessing the attribute of the class, i.e., Dataset.geo
    186             return self._accessor
--> 187         accessor_obj = self._accessor(obj)
    188         # Replace the property with the accessor object. Inspired by:
    189         # http://www.pydanny.com/cached-property.html

~/anaconda3/lib/python3.7/site-packages/pandas/core/strings.py in __init__(self, data)
   2037 
   2038     def __init__(self, data):
-> 2039         self._inferred_dtype = self._validate(data)
   2040         self._is_categorical = is_categorical_dtype(data)
   2041         self._is_string = data.dtype.name == "string"

~/anaconda3/lib/python3.7/site-packages/pandas/core/strings.py in _validate(data)
   2094 
   2095         if inferred_dtype not in allowed_types:
-> 2096             raise AttributeError("Can only use .str accessor with string values!")
   2097         return inferred_dtype
   2098 

AttributeError: Can only use .str accessor with string values!

Checking if fire_numbers is numeric

In [50]:
df['fire_numbers'].str.isnumeric()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-cc45f36d1752> in <module>
----> 1 df['fire_numbers'].str.isnumeric()

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5268             or name in self._accessors
   5269         ):
-> 5270             return object.__getattribute__(self, name)
   5271         else:
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):

~/anaconda3/lib/python3.7/site-packages/pandas/core/accessor.py in __get__(self, obj, cls)
    185             # we're accessing the attribute of the class, i.e., Dataset.geo
    186             return self._accessor
--> 187         accessor_obj = self._accessor(obj)
    188         # Replace the property with the accessor object. Inspired by:
    189         # http://www.pydanny.com/cached-property.html

~/anaconda3/lib/python3.7/site-packages/pandas/core/strings.py in __init__(self, data)
   2037 
   2038     def __init__(self, data):
-> 2039         self._inferred_dtype = self._validate(data)
   2040         self._is_categorical = is_categorical_dtype(data)
   2041         self._is_string = data.dtype.name == "string"

~/anaconda3/lib/python3.7/site-packages/pandas/core/strings.py in _validate(data)
   2094 
   2095         if inferred_dtype not in allowed_types:
-> 2096             raise AttributeError("Can only use .str accessor with string values!")
   2097         return inferred_dtype
   2098 

AttributeError: Can only use .str accessor with string values!

Solution: Converting to String

Checking if fire_numbers is digit

In [51]:
df['fire_numbers'].astype(str).str.isdigit() 
Out[51]:
0       False
1       False
2       False
3       False
4       False
        ...  
6447    False
6448    False
6449    False
6450    False
6451    False
Name: fire_numbers, Length: 6452, dtype: bool

Checking if fire_numbers is numeric

In [52]:
df['fire_numbers'].astype(str).str.isnumeric()
Out[52]:
0       False
1       False
2       False
3       False
4       False
        ...  
6447    False
6448    False
6449    False
6450    False
6451    False
Name: fire_numbers, Length: 6452, dtype: bool