导入 Pandas 出现错误 AttributeError:模块 'pandas' 在 iPython Notebook 中没有属性 'core'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36521691/
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-08-19 17:59:22  来源:igfitidea点击:

Importing Pandas gives error AttributeError: module 'pandas' has no attribute 'core' in iPython Notebook

pythonpandasanaconda

提问by Bert Carremans

I am running a iPython notebook via the Anaconda Navigator app (version 1.1.0). When I want to import pandas it gives me a strange error. I thought the Anaconda app included the pandas package?

我正在通过 Anaconda Navigator 应用程序(版本 1.1.0)运行 iPython 笔记本。当我想导入熊猫时,它给了我一个奇怪的错误。我以为 Anaconda 应用程序包含 pandas 包?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-af55e7023913> in <module>()
----> 1 import pandas as pd

/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/__init__.py in <module>()
 37 import pandas.core.config_init
 38 
---> 39 from pandas.core.api import *
 40 from pandas.sparse.api import *
 41 from pandas.stats.api import *

/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/api.py in <module>()
  8 from pandas.core.common import isnull, notnull
  9 from pandas.core.categorical import Categorical
---> 10 from pandas.core.groupby import Grouper
 11 from pandas.core.format import set_eng_float_format
 12 from pandas.core.index import (Index, CategoricalIndex, Int64Index,

/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in <module>()
 16                               DataError, SpecificationError)
 17 from pandas.core.categorical import Categorical
---> 18 from pandas.core.frame import DataFrame
 19 from pandas.core.generic import NDFrame
 20 from pandas.core.index import (Index, MultiIndex, CategoricalIndex,

/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in <module>()
 37                                    create_block_manager_from_arrays,
 38                                    create_block_manager_from_blocks)
---> 39 from pandas.core.series import Series
 40 from pandas.core.categorical import Categorical
 41 import pandas.computation.expressions as expressions

/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/series.py in <module>()
 33 from pandas.core.internals import SingleBlockManager
 34 from pandas.core.categorical import Categorical, CategoricalAccessor
---> 35 import pandas.core.strings as strings
 36 from pandas.tseries.common import (maybe_to_datetimelike,
 37                                    CombinedDatetimelikeProperties)

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

采纳答案by Bert Carremans

Apparently the error I got when trying to import pandas for the first timewas ValueError: unknown locale: UTF-8

显然,我第一次尝试导入熊猫时遇到的错误是ValueError: unknown locale: UTF-8

Trying to import again afterwards, gave another error as described in my question above.

之后尝试再次导入,出现了另一个错误,如我上面的问题所述。

I found the solution to solve the ValueError on IPython Notebook locale error

我找到了解决IPython Notebook 语言环境错误上的 ValueError 的解决方案

After updating my bash profile, the error AttributeError: module 'pandas' has no attribute 'core'did not appear anymore.

更新我的 bash 配置文件后,错误AttributeError: module 'pandas' has no attribute 'core'不再出现。

回答by Dataman

"Have you tried turning it off and on again?" (Roy of The IT crowd)

你试过把它关掉再打开吗?”(罗伊的IT 人群

This happened to me today, which is why I ended up to this page. Seeing that error was weird since, recently, I have not made any changes in my Python environment. Interestingly, I observed that if I open a new notebook and import pandasI would not get the same error message. So, I did shutdownthe troublesome notebook and started it again and voila it is working again!

这件事今天发生在我身上,这就是为什么我结束了这个页面。看到这个错误很奇怪,因为最近我没有对我的 Python 环境进行任何更改。有趣的是,我观察到如果我打开一个新笔记本,import pandas我不会收到相同的错误消息。所以,我做shutdown了麻烦的笔记本并重新启动它,瞧它又开始工作了!

Even though this solved the problem (at least for me), I cannot readily come up with an explanation as to why it happened in the first place!

尽管这解决了问题(至少对我而言),但我无法轻易解释为什么它首先发生!

回答by Rohan Raghuwanshi

There's this bug in the latest version of pandas (pandas 0.23) that gives you an error on importing pandas.

最新版本的 Pandas (pandas 0.23) 中存在这个错误,导致您在导入 Pandas 时出错。

But this can be easily fixed by installing an earlier version of pandas (pandas 0.22) using the command pip install pandas==0.22on Windows Command Prompt.

但这可以通过使用pip install pandas==0.22Windows 命令提示符上的命令安装早期版本的 Pandas (pandas 0.22) 来轻松解决。

回答by Senthilkumar Gopal

There is an other weird reason this happens. If you have a file called pandas.py or a directory called pandas in the same or nested levels, that library is used instead and fails to work. Rename the folder and restart the env and it started working. Faced this

发生这种情况还有另一个奇怪的原因。如果您在相同或嵌套级别中有一个名为 pandas.py 的文件或名为 pandas 的目录,则会使用该库并且无法工作。重命名文件夹并重新启动 env 并开始工作。面对这个

回答by Josh Peak

I had a similar issue since I installed pandasusing python -m pip install pandas --upgrade --userwhich installed a conflicting version in my user python packages directory, masking the Anaconda installed version that other dependencies relied upon.

我有一个类似的问题,因为我安装pandasusing python -m pip install pandas --upgrade --userwhich 在我的用户 python 包目录中安装了一个冲突的版本,掩盖了其他依赖项所依赖的 Anaconda 安装版本。

conda list | grep pandas
pandas == 0.23.4

python -m pip list | grep pandas
pandas == 0.24.0

So uninstalling the user directory masked version cleaned up the issue for me.

所以卸载用户目录屏蔽版本为我清理了这个问题。

python -m pip uninstall pandas

For reference all possible python packages are installed in the directories listed from this command:

作为参考,所有可能的 python 包都安装在此命令列出的目录中:

python -m site

Might be worth iterating through these and checking for duplicates.

可能值得遍历这些并检查重复项。

Edit: Since my original answer I learnt you can run:

编辑:自从我原来的答案我了解到你可以运行:

python -m pip list -v

And it shows the directory the library is installed. This often shows whether the library you want is in a virtual environment, conda environment, user directory, system site packages etc.

它显示了库的安装目录。这通常会显示您想要的库是否在虚拟环境、conda 环境、用户目录、系统站点包等中。

回答by Maddy Anand

You are getting this is because you are using a Anaconda distribution of Jupyter notebook. So just do conda install pandasrestart your jupyter notebookand rerun your cell. It should work. If you are trying this on a Virtual Env try this

您之所以会出现这种情况,是因为您使用的是 Jupyter Notebook 的 Anaconda 发行版。因此,只需conda install pandas重新启动jupyter notebook并重新运行单元即可。它应该工作。如果您在 Virtual Env 上尝试此操作,请尝试此操作

  1. conda create -n name_of_my_env pythonThis will create a minimal environment with only Python installed in it. To put your self inside this environment run:
  1. conda create -n name_of_my_env python这将创建一个仅安装 Python 的最小环境。把你自己放在这个环境中运行:

2 source activate name_of_my_envOn Windows the command is: activate name_of_my_envThe final step required is to install pandas. This can be done with the following command:

2source activate name_of_my_env在 Windows 上,命令是: activate name_of_my_env所需的最后一步是安装 Pandas。这可以通过以下命令完成:

conda install pandasTo install a specific pandas version:

conda install pandas要安装特定的 Pandas 版本:

conda install pandas=0.20.3

conda install pandas=0.20.3

To install other packages, IPython for example:

要安装其他软件包,例如 IPython:

conda install ipythonTo install the full Anaconda distribution:

conda install ipython要安装完整的 Anaconda 发行版:

conda install anaconda

conda install anaconda

If you need packages that are available to pip but not conda, then install pip, and then use pip to install those packages:

如果您需要可用于 pip 但不可用于 conda 的软件包,请安装 pip,然后使用 pip 安装这些软件包:

conda install pippip install djangoInstalling from PyPIpandas can be installed via pip from PyPI.

conda install pippip install djangoInstalling from PyPIpandas 可以通过 PyPI 的 pip 安装。

pip install pandasInstalling with ActivePython

pip install pandas使用 ActivePython 安装

Hope this helps.

希望这可以帮助。

回答by Niraj D Pandey

I face similar issue while importing TensorFlow. If you are using Tensorflow which uses Pandas library, I suggest restarting your kernel of Anaconda. This works for me.

我在导入时遇到类似的问题TensorFlow。如果您使用的是使用 Pandas 库的 Tensorflow,我建议您使用restarting your kernel of Anaconda. 这对我有用。

回答by CathyQian

I recently came across the same problem right after I installed Pandas 0.23 in Anaconda Prompt. The solution is simply to restart the Jupyter Notebook which reports the error. May it helps.

我最近在 Anaconda Prompt 中安装了 Pandas 0.23 后就遇到了同样的问题。解决方案只是重新启动报告错误的 Jupyter Notebook。可能会有所帮助。

回答by dooms

Try in your console

在您的控制台中尝试

conda install pandas

and see what's the message given.

看看给出的信息是什么。

回答by koalagreener

I have just solved this problem. Recently, I changed my language setting of my MacBook from English-UK to Chinese. And I suppose that setting will also change the setting in the "locale." Becuase when I switched back, I found that the setting of locale had been changed again, and I am fine to import the pandas again,.

我刚刚解决了这个问题。最近,我将 MacBook 的语言设置从英语-英国更改为中文。而且我想该设置也会更改“语言环境”中的设置。因为当我切换回来时,我发现语言环境的设置又被更改了,我可以再次导入pandas,。

So if you have changed the language setting recently, you may worth to have a try change it back.

因此,如果您最近更改了语言设置,您可能值得尝试将其改回。