If you encounter the AttributeError: ‘list’ object has no attribute ‘astype’ in Python, you can refer to our following article for a solution. Let’s go into detail now.
What causes the AttributeError: ‘list’ object has no attribute ‘astype’ in Python?
The astype()
method casts the data type (dtype) in a pandas object. The method supports any data type supported by Numpy.
AttributeError is one of the exceptions in Python. AttributeError occurs when you access an undefined property on an object.
The AttributeError: ‘list’ object has no attribute ‘astype’ happens because you call the astype() method on the list.
Example:
myList = [1.1, 1.2, 1.3, 1.4] print(myList.astype(str))
Output:
Traceback (most recent call last):
File "/prog.py", line 2, in <module>
print(myList.astype(str))
AttributeError: 'list' object has no attribute 'astype'
How to solve this error?
Convert the list to a numpy array
Converting the list to a numpy array is your handling of the error.
Syntax:
np.array()
The np.array() function returns a numpy array (the number of dimensions you initialize).
Example:
- Import the numpy module.
- Create a list.
- Convert the list to a numpy array using the np.array() function.
- Use the astype attribute to cast numpy arrays.
import numpy as np myList = [1.1, 1.2, 1.3, 1.4] # Convert the list to a numpy array npArrayObj = np.array(myList) # Use the astype attribute to cast numpy arrays result = npArrayObj.astype(dtype=float) print('Result after casting:', result)
Output:
Result after casting: [1.1 1.2 1.3 1.4]
Use the dictionary data structure type instead of the list data type
Because pandas astype() supports changing multiple data types using Dict, you can use it this way.
Syntax:
DataFrame.astype(dtype, copy=True, errors="raise")
Parameters:
- dtype: Using a numpy.dtype or Python type passes all pandas objects that appear to have the same type.
- copy =True: Default is True. Returns a copy when copy=True.
- errors : Control the exception that occurs. Use ‘raise’ to raise an exception when the input is invalid. ‘ignore’ returns the original object when an error occurs.
Example:
- Import numpy and pandas modules.
- Initialize a dictionary of keys and values that are numpy arrays.
- Create a dataframe from dict.
- Use the astype attribute to cast numpy arrays.
import pandas as pd import numpy as np # Create DataFrame from Dictionary myDic = { 'array1': [1.1, 1.2, 1.3], 'array2': [2.1, 2.2, 2.3], 'array3': [3.1, 3.2, 3.3], } # Create a dataframe from dict df = pd.DataFrame(myDic) # Use the astype attribute to cast numpy arrays df2 = df.astype({'array1':'float','array2':'float','array3':'float'}) print(df2)
Output:
array1 array2 array3
0 1.1 2.1 3.1
1 1.2 2.2 3.2
2 1.3 2.3 3.3
Summary
I hope you understand the AttributeError: ‘list’ object has no attribute ‘astype’ in Python and can fix it quickly.
Maybe you are interested:
- AttributeError: ‘NoneType’ object has no attribute ‘split’ in Python
- AttributeError: ‘dict’ object has no attribute ‘id’ in Python
- AttributeError: module ‘sipbuild.api’ has no attribute ‘prepare_metadata_for_build_wheel’

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java