votes up 1

Can only use .str accessor with string values!

Package:
pandas
github stars 30911
Exception Class:
AttributeError

Raise code

values = getattr(data, "values", data)  # Series / Index
        values = getattr(values, "categories", values)  # categorical / normal

        inferred_dtype = lib.infer_dtype(values, skipna=True)

        if inferred_dtype not in allowed_types:
            raise AttributeError("Can only use .str accessor with string values!")
        return inferred_dtype

    def __getitem__(self, key):
        result = self._data.array._str_getitem(key)
        return self._wrap_result(result)

    def __iter__(self):

Ways to fix

votes up 2 votes down

This error is raised when the str attribute is called on a pandas column with a non string data type. The str accessor works only on columns whose data is a string type.

Here is how to reproduce the exception.

  • Setup your virtual environment

Install pipenv

$ pip install --user pipenv  

$ mkdir testEnv

$ cd testEnv

$ pipenv install pandas

$ pipenv shell

Then run the following code inside the virtual environment.

import pandas as pd
d = {'name': ['apple', 'orange'], 'price': [1.3, 1.4]}
df = pd.DataFrame(data=d)
print(df)
# We want to remove the decimal point (.).
new_series = df['price'].str.replace('.','')
new_df = pd.DataFrame(new_series,columns=["name","price"])
new_df["name"] = df["name"]
print("\n\n",new_df)

The error message

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-f53fc8122c19> in <module>()  
4 print(df)  
5 # We want to remove the decimal point (.). 
----> 6 new_series = df['price'].str.replace('.','')  
7 new_df = pd.DataFrame(new_series,columns=["name","price"])  
8 print("\n\n",new_df) 
.
.
.
/usr/local/lib/python3.7/dist-packages/pandas/core/strings.py in _validate(data)  2155   2156 if inferred_dtype not in allowed_types: -> 2157 raise AttributeError("Can only use .str accessor with string values!")  2158 return inferred_dtype  2159  
AttributeError: Can only use .str accessor with string values!

Fixed version of the code:

To avoid raising this exception or to fix this scenario use the following code instead.

Notice the line new_series =df['price'].apply(str).str.replace('.','')

That is the solution.

import pandas as pd
d = {'name': ['apple', 'orange'], 'price': [1.3, 1.4]}
df = pd.DataFrame(data=d)
print(df)
# We want to remove the decimal point (.).
new_series =df['price'].apply(str).str.replace('.','')
new_df = pd.DataFrame(new_series,columns=["name","price"])
new_df["name"] = df["name"]
print("\n\n",new_df)

  name  price 
0 apple 1.3 
1 orange 1.4 

  name   price
0 apple    13
1 orange   14

Jan 14, 2022 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix