Last updated: Apr 8, 2024
Reading time·8 min
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
.
Here is an example of how the error occurs in a file called 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.
DataFrame
as class names are case-sensitive.Make sure to rename your local file to something other than pandas.py
to solve
the error.
import pandas as pd # ✅ works df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)
Your file could be named main.py
or any other name that doesn't clash with
another module.
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.
You can access the __file__
property on the imported module to see whether it
is shadowed by a local file.
import pandas as pd # ⛔️ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/pandas.py # ✅ The result is pulling in the 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.
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.
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.
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.
The Python "AttributeError module 'pandas' has no attribute 'read_csv'" occurs
when we have a local file named pandas.py
or csv.py
and try to import from
the pandas
module.
To solve the error, make sure to rename any local files named pandas.py
.
Here is an example of how the error occurs in a file called pandas.py
.
import pandas as pd # ⛔️ AttributeError: module 'pandas' has no attribute 'read_csv' df = pd.read_csv('employees.csv') print(df)
pandas.py
or csv.py
The most likely cause of the error is having a local file named pandas.py
which shadows the official pandas
module.
The error is also caused by having a local file named csv.py
.
Make sure to rename your local file to something other than pandas.py
to solve
the error.
import pandas as pd df = pd.read_csv('employees.csv', sep=',', encoding='utf-8') # first_name last_name # 0 Alice Smith # 1 Bobby Hadz # 2 Carl Smith print(df)
The code sample above assumes that you have an employees.csv
file that looks
as follows.
first_name,last_name Alice,Smith Bobby,Hadz Carl,Smith
The employees.csv
file and the main.py
file should be located in the same
directory.
Another thing to look out for is having an incorrect import statement.
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.
You can access the __file__
property on the imported module to see whether it
is shadowed by a local file.
import pandas as pd # ⛔️ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/pandas.py # ✅ The result is pulling in the 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.
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.
We can see that the imported pandas
module doesn't have read_csv
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.
import pandas as pd # ⛔️ AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import) df = pd.read_csv('employees.csv') print(df)
Renaming your file solves the error.
The Python "AttributeError module 'pandas' has no attribute 'Series'" occurs
when we have a local file named pandas.py
or misspell Series
(case-sensitive).
To solve the error, make sure to rename any local files named pandas.py
.
Here is an example of how the error occurs in a file called pandas.py
.
import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} # ⛔️ AttributeError: module 'pandas' has no attribute 'Series' ser = pd.Series(data=d, index=['a', 'b', 'c']) print(ser)
pandas.py
The most likely cause of the error is having a local file named pandas.py
which shadows the official pandas
module.
Series
as class names are case-sensitive.Make sure to rename your local file to something other than pandas.py
to solve
the error.
import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} # ✅ works ser = pd.Series(data=d, index=['a', 'b', 'c']) print(ser)
The name of your local file should not clash with the name of a module.
Otherwise, the local module shadows the remote module which causes issues.
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 Series
class as
pd.Series.
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.
You can access the __file__
property on the imported module to see whether it
is shadowed by a local file.
import pandas as pd # ⛔️ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/pandas.py # ✅ The result is pulling in the 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.
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.
We can see that the imported pandas
module doesn't have Series
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.
import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} # ⛔️ AttributeError: partially initialized module 'pandas' has no attribute 'Series' (most likely due to a circular import) ser = pd.Series(data=d, index=['a', 'b', 'c']) print(ser)
Renaming your file solves the error.
You can learn more about the related topics by checking out the following tutorials: