8

My Jupiter notebook was crushed, so I have to reinstall the notebook, but in the new Jupiter notebook, I cannot run pandas.

import pandas as pd
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6860/4080736814.py in <module>
----> 1 import pandas as pd

~\anaconda3\lib\site-packages\pandas\__init__.py in <module>
     20 
     21 # numpy compat
---> 22 from pandas.compat import (
     23     np_version_under1p18 as _np_version_under1p18,
     24     is_numpy_dev as _is_numpy_dev,

~\anaconda3\lib\site-packages\pandas\compat\__init__.py in <module>
     12 import warnings
     13 
---> 14 from pandas._typing import F
     15 from pandas.compat.numpy import (
     16     is_numpy_dev,

~\anaconda3\lib\site-packages\pandas\_typing.py in <module>
     82 # array-like
     83 
---> 84 ArrayLike = Union["ExtensionArray", np.ndarray]
     85 AnyArrayLike = Union[ArrayLike, "Index", "Series"]
     86 

AttributeError: module 'numpy' has no attribute 'ndarray'

I have tried to rename or delete the numpy.py, but it didn't work.

feetwet
  • 3,248
  • 7
  • 46
  • 84
Yumeng Xu
  • 179
  • 1
  • 2
  • 11

3 Answers3

4

The problem is not with Pandas. This is due to NumPy. I had a similar issue and this is what I did.

Ran python -c "import numpy as np; print(np.__file__); print(np.ndarray)". The expect output will contain the location of your installed NumPy package and <class 'numpy.ndarray'> for the second print statement. In my case, it seemed that NumPy package was located in ~/.local.

I tried reinstalling NumPy in conda, but the location was still the same. So, as per this solution, I deleted ~/.local. Then the error disappeared.

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
Gautam Sreekumar
  • 536
  • 1
  • 9
  • 20
-1

In my case I had a script called numpy.py.

Answer found here -> https://discuss.python.org/t/numpy-y-pandas-problem-import/13460/2

Jamie Armour
  • 524
  • 5
  • 10
-2

Just try to degrade or upgrade the numpy versions. It works for me...

  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '22 at 23:04