How To Fix “DataFrame constructor not properly called!”

DataFrame constructor not properly called!

Pandas doesn’t accept every data type to create a DataFrame. Sometimes it will give you the error “DataFrame constructor not properly called!”. Read the examples below to understand why it happens.

The Error “DataFrame constructor not properly called!”

You are getting this error because, as the message implies, there is something wrong with the way the DataFrame() function is called. And there are many of reasons for this.

Slow down for a moment and have a look at this constructor again:

pandas.DataFrame(data, index, columns, dtype, copy)

This function will create a DataFrame, the primary data structure of the pandas module, from the content of the data parameter. This is the only required parameter of the function.

Pandas allows you to create DataFrames from various data structures. They can be Python iterables, dicts, other Pandas DataFrames, or NumPy ndarrays. If the data parameter is a dict, it must contain list-like objects like arrays, dataclass, constant, or Pandas Series.

Most of the time, pandas.DataFrame() returns the error “DataFrame constructor not properly called!” when you don’t follow those rules properly.

Examples And Solutions

Example 1

Pandas uses DataFrames to store data in a two-dimensional structure, just like columns and rows in a table. When the source of data you provide can’t be used to construct such a structure, you will end up with errors.

For instance, don’t use regular strings or numbers with the DataFrame() constructor:

>>> pd.DataFrame("LearnShareIT")
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!

If you need to save only a string or number element into a DataFrame, put it into a list or dict instead:

>>> pd.DataFrame(["LearnShareIT"])
              0
0  LearnShareIT
>>> pd.DataFrame({10})
    0
0  10

Example 2

Another example is when you read a JSON file with open():

with open('example.json', 'r') as file:
    filedata = json.load(file)
    df = pd.DataFrame(file_list)

The DataFrame() constructor might not be able to get the data it needs from the json.load() function and generate an errors.

Pandas has a built-in solution for reading JSON files. You should use that instead:

df = pd.read_json('example.json')

Example 3

While Spark draws inspiration from DataFrames of Pandas, you can use them directly to convert to DataFrame in PySpark:

from pyspark.sql import Row
df = spark.createDataFrame([
    Row(a = 'LearnShareIT', b = 'tutorials', c = 'learnshareit.com'),
    Row(a = 'Quora', b = 'Q&A', c = 'quora.com')
])
 
df.show()
df2 = pd.DataFrame(df)

Output:

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!

Spark has its own function for this purpose – toPandas():

>>> df2 = df.toPandas()
>>> print(df2)
              a          b                 c
0  LearnShareIT  tutorials  learnshareit.com
1         Quora        Q&A         quora.com

Remember that this function is only designed for small DataFrames, as Spark loads all of their elements to the drive’s memory.

Summary

Pandas will returns the error “DataFrame constructor not properly called!” when you don’t provide the correct data type to the DataFrame() constructor. Make sure it is an iterable, an ndarray, or a DataFrame.

Maybe you are interested in similar errors:

Leave a Reply

Your email address will not be published. Required fields are marked *