AttributeError module 'pandas' has no attribute 'DataFrame'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError module 'pandas' has no attribute 'DataFrame' #

The Python "AttributeError module 'pandas' has no attribute 'DataFrame'" occurs when we have a local file named pandas.py or misspell DataFrame. To solve the error, make sure to rename any local files named pandas.py.

attributeerror module pandas has no attribute dataframe

Here is an example of how the error occurs in a file called pandas.py.

pandas.py
import pandas as pd # ⛔️ AttributeError: module 'pandas' has no attribute 'DataFrame' df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)

The most likely cause of the error is having a local file named pandas.py which shadows the official pandas module.

Make sure you haven't misspelled DataFrame as class names are case-sensitive.

Make sure to rename your local file to something other than pandas.py to solve the error.

main.py
import pandas as pd # ✅ works df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)

Another thing to look out for is having an incorrect import statement.

In the example, we import the pandas module and alias it to pd, so we would access the DataFrame class as pd.DataFrame.

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import pandas as pd # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/pandas.py # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/pandas/__init__.py print(pd.__file__)

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the pandas module looks like when I have a file pandas.py in the same directory.

pandas.py
import pandas as pd # ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(pd))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported pandas module doesn't have DataFrame attribute, which makes it evident that we are shadowing the official pandas module with our local pandas.py file.

If you try to import the pandas module in a file called pandas.py, you would get a little different error message that means the same thing.

pandas.py
import pandas as pd # ⛔️ AttributeError: partially initialized module 'pandas' has no attribute 'DataFrame' (most likely due to a circular import) df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } )

Renaming your file solves the error.

Conclusion #

The Python "AttributeError module 'pandas' has no attribute 'DataFrame'" occurs when we have a local file named pandas.py or misspell DataFrame. To solve the error, make sure to rename any local files named pandas.py.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.