. Advertisement .
..3..
. Advertisement .
..4..
The “ValueError: DataFrame constructor not properly called!” error message can be hard to debug because of its unclear nature. Read on to explore common scenarios that may lead to this error.
The “ValueError: DataFrame constructor not properly called!” Error
By printing this error, pandas indicates something has gone wrong with the constructor pandas.DataFrame(). To understand common mistakes developers make when creating a DataFrame, you must keep in mind the correct syntax of this constructor:
>>> pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)
Only data is required, while the rest of them are optional parameters. The pandas.DataFrame() constructor accepts many objects in Python for its data source: ndarray, dict, iterable, and another DataFrame.
The error in question occurs when you don’t provide the constructor with proper parameters.
Example:
>>> data = {'Sites' : ['ITTutoria', 'Stack Overflow', 'Quora'],
... 'Ranking' : [1, 2, 3]}
>>> data
{'Sites': ['ITTutoria', 'Stack Overflow', 'Quora'], 'Ranking': [1, 2, 3]}
>>> df = pd.DataFrame("data")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
raise ValueError("DataFrame constructor not properly called!")
You are passing a dictionary into the constructor, which is totally fine. Your mistake is the representation of that dict. When given a str value like the common above, the DataFrame() will raise a ValueError exception.
You can fix the problem by calling the constructor and passing the dict directly instead of the str value:
>>> df = pd.DataFrame(data)
>>> df
Sites Ranking
0 ITTutoria 1
1 Stack Overflow 2
2 Quora 3
In a similar fashion, you should not pass data types like numbers into DataFrame():
>>> df = pd.DataFrame(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/site-packages/pandas/core/frame.py", line 756, in __init__
raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!
These mistakes might look too trivia and impossible to make. But in a complicated program, you might lose control over the object passed into the constructor, resulting in the error.
Another common mishap many suffer is when they process JSON data.
Example:
>>> data = '{ "ranking": 1, "language": "Python", "site": "ITTutoria"}'
>>> df = pd.DataFrame(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/site-packages/pandas/core/frame.py", line 756, in __init__
raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!
While data seems to be legit JSON data and can be given into a DataFrame constructor, this is not the case. You are just passing it like a normal string, which isn’t acceptable.
To deal with JSON, pandas has a dedicated function – pandas.read_json(). It acts the middle and processes the JSON data into a pandas DataFrame:
>>> data = '{ "ranking": 1, "language": "Python", "site": "ITTutoria"}'
>>> pf = pd.read_json(data, orient ='index')
>>> pdf
0
ranking 1
language P ython
site ITTutoria
Likewise, you should take advantage of built-in solutions from the pandas library when dealing with CSV files as well.
Suppose you have a data.csv file with this content:
ranking, site, language
1, ITTutoria, Python
This example will return the same error:
import pandas as pd
f = open("data.csv", "r")
df = pd.DataFrame(f.read())
print(df)
You should use the pandas.read_csv() function to create a DataFrame:
>>> df = pd.read_csv("data.csv")
>>> df
ranking site language
0 1 ITTutoria P ython
>>> type(df)
<class 'pandas.core.frame.DataFrame'>
Conclusion
The “ValueError: DataFrame constructor not properly called!” message doesn’t tell us which specific error we are making. But most of the time, it happens because the constructor has been given the wrong data type. Replace it, and your program should create a DataFrame without any issue.
Leave a comment