AttributeError: 模块“pandas”没有属性“DataFrame”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53757154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:12:27  来源:igfitidea点击:

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

pythonwindowspandasanacondajupyter

提问by Tafseer Ahmed

I am on Windows 10, using Anaconda (Anaconda3-5.3.1-Windows-x86_64). I have not been able to resolve this error even after reinstalling Anaconda. I don't believe adding conda to PATH could be causing this.

我在 Windows 10 上,使用 Anaconda (Anaconda3-5.3.1-Windows-x86_64)。即使在重新安装 Anaconda 后,我也无法解决此错误。我不相信将 conda 添加到 PATH 可能会导致这种情况。

(base) D:\KZ\Projects\Custom Vision>conda --version conda 4.5.11

(base) D:\KZ\Projects\Custom Vision>conda --version conda 4.5.11

    (base) D:\KZ\Projects\Custom Vision>python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import pandas as pd
  File "D:\Users\KZ\Anaconda3\lib\site-packages\pandas\__init__.py", line 42, in <module>
    from pandas.core.api import *
  File "D:\Users\KZ\Anaconda3\lib\site-packages\pandas\core\api.py", line 10, in <module>
    from pandas.core.groupby.groupby import Grouper
  File "D:\Users\KZ\Anaconda3\lib\site-packages\pandas\core\groupby\__init__.py", line 2, in <module>
    from pandas.core.groupby.groupby import (
  File "D:\Users\KZ\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py", line 7, in <module>
    import copy
  File "D:\KZ\Projects\Custom Vision\copy.py", line 13, in <module>
    df=pd.DataFrame()
AttributeError: module 'pandas' has no attribute 'DataFrame'

(base) D:\KZ\Projects\Custom Vision>

Here is what is inside test.py

这是 test.py 里面的内容

import pandas as pd 

df = pd.DataFrame()
df["test"]=pd.Series[list("abcd")]

However if I python from terminal and import pandas, it works fine. It only causes this issue when I execute the script.

但是,如果我从终端 python 并导入Pandas,它工作正常。它仅在我执行脚本时导致此问题。

(base) C:\Users\KZ>python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df
Empty DataFrame
Columns: []
Index: []
>>> exit ()

回答by Lyxthe Lyxos

how I'm understanding it (correct me if I'm wrong) your test.py is a bit more complicate than the one you gave here. Especially concerning the 'impot copy' part that is shown on the execution log. If I'm right, you have an import copy after your import pandas as pd in your test.py file. I think, since copy is also a keyword used in pandas, maybe the name of your copy.py is messing something with pandas. Try to remove it just for the test to see if the error is still there.

我是如何理解它的(如果我错了请纠正我)你的 test.py 比你在这里给出的要复杂一些。特别是关于执行日志中显示的“导入复制”部分。如果我是对的,那么在 test.py 文件中将Pandas导入为 pd 之后,您就有了一个导入副本。我认为,由于 copy 也是 Pandas 中使用的关键字,因此您的 copy.py 名称可能会与 Pandas 混淆。尝试将其删除以进行测试以查看错误是否仍然存在。

回答by Manideep Karthik

See by including () in pd.Series

通过在 pd.Series 中包含 () 来查看

import pandas as pd 

df = pd.DataFrame()
df["test"]=pd.Series([list("abcd")])