'module 'numpy' has no attribute 'ndarray'

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.



Solution 1:[1]

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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gautam Sreekumar
Solution 2 Amit Kumar