How to Solve Python AttributeError: Can only use .str accessor with string values!

by | Programming, Python, Tips

The string accessor .str is an attribute of the Pandas Series class and provides functions to work with string data. If you try to use the .str accessor attribute on a column of a Pandas DataFrame or a Series and the values are not strings, you will raise the AttributeError: Can only use .str accessor with string values!

To solve this error, you can cast the values in the Series to string type using .astype(str) function before using .str. For example: df['column_name'] = df['column_name'].astype(str).str.replace('.', '').

This tutorial will go through the error in detail and how to solve it with code examples.


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

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part ‘Can only use .str accessor with string values‘ tells us that the str accessor is only suitable for a Series containing only string values. The data type of a column containing string values is object. We can show this by defining a DataFrame with different types of value in each column and using the DataFrame.dtypes attribute to get the dtype of each column.

df_check = pd.DataFrame({'float':[2.4],
'int':[5],
'datetime': [pd.Timestamp('20210310')],
'string': ['python']})

print(df_check.dtypes)
float              float64
int                  int64
datetime    datetime64[ns]
string              object
dtype: object

We can see that the ‘string‘ column containing a string value has the object dtype. It is useful to check the dtype of the column before attempting to use the .str attribute.

Example

Let’s look at an example where we have a DataFrame containing pizzas and their prices.

import pandas as pd

# Create DataFrame

df = pd.DataFrame({'pizza':['margherita', 'pepperoni', 'four cheeses', 'hawaiian', 'parmigiana'],
'price':[7.99, 8.99, 10.99, 8.99, 11.99]})

print(df)
 pizza  price
0    margherita   7.99
1     pepperoni   8.99
2  four cheeses  10.99
3      hawaiian   8.99
4    parmigiana  11.99

We want to replace ‘.99‘ with ‘.00‘ to make the pizza prices cheaper. We will attempt to replace the numbers by calling str.replace on the column df['price']. Let’s look at the code:

# Replace substring 
df['price'] = df['price'].str.replace('99','00')
AttributeError: Can only use .str accessor with string values!

The error occurs because the dtype of the Series is not object. We can verify the dtype of the different columns in the DataFrame by using the DataFrame.dtypes attribute

pizza     object
price    float64
dtype: object

The price column is of type float64. The .str accessor is only suitable for string values.

Solution

We can cast the Series to string using the .astype() function to solve this error. Let’s look at the revised code:

import pandas as pd

# Create DataFrame

df = pd.DataFrame({'pizza':['margherita', 'pepperoni', 'four cheeses', 'hawaiian', 'parmigiana'],
'price':[7.99, 8.99, 10.99, 8.99, 11.99]})

# Replace substring

df['price'] = df['price'].astype(str).str.replace('99','00')

print(df)

print(df.dtypes)
        pizza  price
0    margherita   7.00
1     pepperoni   8.00
2  four cheeses  10.00
3      hawaiian   8.00
4    parmigiana  11.00

pizza    object
price    object
dtype: object

We can see that after using astype(str) the df['price'] column is object dtype. We are then able to use str.replace() to replace the ‘99‘ substring with ‘00‘.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: Can only use .str accessor with string values! occurs when you try to use the string accessor attribute .str , but the Series contains values that are not strings. You can solve this by casting the Series to string using astype(str).

For further reading on errors involving Pandas, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee