attributeerror: ‘str’ object has no attribute ‘values’

As a Python developer, you might have encountered this error message “AttributeError: ‘str’ object has no attribute ‘values’“. This error typically occurs when you are trying to access a non-existent attribute or method of an ‘str‘ object in Python.

In this article, we will discuss the causes and solutions of this error, as well as provide examples to explain its usage.

How does the error ‘str’ object has no attribute ‘values’ occur?

This error usually occurs when you try to access the “.values” attribute of a string object in Python. The “.values” attribute is used to access the values of a dictionary or pandas data frame, and it is not a valid attribute for a string object.

For example, we assume you have a variable my_string that consists of a string value. When you try to access the “.values” attribute of my_string like this:

my_string = "Hello, world!"
print(my_string.values)

If you run the following code it will raise the following error output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 2, in
print(my_string.values)
AttributeError: ‘str’ object has no attribute ‘values’

What are the Causes of Attribute Error: ‘str’ Object has no Attribute values?

There are multiple possible causes of this error, which include the following:

  • Accessing Non-existent Attribute:
    • One of the common possible causes of the AttributeError is attempting to access an attribute or method that doesn’t exist on the object.
  • Misspelling Attribute Name:
    • Another common possible cause of the AttributeError is misspelling the attribute or method name when you are trying to access it
  • Incorrect Data Type:
    • Finally, the AttributeError can also be caused by passing an incorrect data type to an object’s method or attribute.

Also read the other resolved python error that you might encounter while running some programs in python:

How to resolve AttributeError: str object has no attribute ‘values’ Error?

Now that you understand the causes of the error, then we will proceed to the solutions on how to resolve it.

To resolve the ‘str’ object has no attribute ‘values’ error. Here are some common solutions to resolve the AttributeError:

Solution 1: Check Attribute Name and Spelling

When the AttributeError is caused by misspelling the attribute name, you can solve it through correcting the spelling of the attribute. Make sure to double-check the attribute name and spelling before trying to access it.

#incorrect
import pandas as pd

# create a simple dataframe
data = {'name': ['Jude', 'Glenn', 'Caren'], 'age': [26, 27, 25]}
df = pd.DataFrame(data)

# access the values of the dataframe using the .values attribute
values = df.valeus
print(values)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 9, in
values = df.valeus
File “C:\Users\Dell\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\generic.py”, line 5902, in getattr
return object.getattribute(self, name)
AttributeError: ‘DataFrame’ object has no attribute ‘valeus’

This will throw an AttributeError because the ‘values’ attribute is misspelled as ‘valeus’.

To solve this error, you will need to use the correct spelling values attribute on a valid object like a dictionary or pandas dataframe, and not on a string object.

#correct way

import pandas as pd

# create a simple dataframe
data = {'name': ['Jude', 'Glenn', 'Caren'], 'age': [26, 27, 25]}
df = pd.DataFrame(data)

# access the values of the dataframe using the .values attribute
values = df.values
print(values)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
[[‘Jude’ 26]
[‘Glenn’ 27]
[‘Caren’ 25]]

Solution 2: Check Data Types

When the AttributeError is caused by passing an incorrect data type to an object’s method or attribute, you can solve it through ensuring the correct data type is passed.

We assume you have a dictionary called my_datatypes and you’re trying to access the values of a key called my_example like this:

my_datatypes = {"my_example": "my_value"}
values = my_datatypes["my_example"].values()

If you run the code it will raise an error output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 2, in
values = my_datatypes[“my_example”].values()
AttributeError: ‘str’ object has no attribute ‘values’

To avoid this error, you can check the type of my_datatypes[“my_example”] before trying to access its values:

my_datatypes = {"my_example": "my_value"}
value = my_datatypes["my_example"]
print(value)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
my_value

Solution 3: Check for Object Type

When the AttributeError is caused by attempting to access an attribute or method that does not exist on the object, you can solve it by checking the object type and making sure that the attribute or method exists on the object type.

For example, if you’re trying to access the ‘values’ attribute of an ‘str’ object, make sure that the ‘values’ attribute exists on the object type.

#incorrect
my_data = "my_value"
if isinstance(my_data, dict):
    values = my_data.values()
else:
    values = my_data.values()
    # This line will raise an AttributeError because a string object doesn't have a `values()` method
print(values)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 5, in
values = my_data.values()
AttributeError: ‘str’ object has no attribute ‘values’

This is the correct way:

my_data = "my_value"
try:
    if isinstance(my_data, dict):
        values = my_data.values()
    else:
        values = [my_data]
    print(values)
except AttributeError:
    print(f"Error: {my_data} object has no attribute 'values()'")

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
[‘my_value’]

Conclusion

To conclude, through following the solutions and examples outlined in this article, you can resolve the attributeerror: ‘str’ object has no attribute ‘values’ error and continue working with your Python code smoothly and efficiently.

Leave a Comment