Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 866
Yumeng Xu Avatar asked Feb 28 '26 11:02

Yumeng Xu


2 Answers

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.

like image 90
Gautam Sreekumar Avatar answered Mar 03 '26 04:03

Gautam Sreekumar


For me none of the above solutions fixed the issue. The only way it worked was I cleaned up all my python environments.

  • Removed manually installed python versions at various locations such as usr/local/bin
  • cleaned up PATH
  • Removed all but python 3.11 from brew.
  • Ran brew cleanup and resolved all warnings/issues.
  • Re-installed command line tools since I had been using the system installed python version for various projects.
  • Created a fresh virtual environment using brew's python3.11 as the base interpreter. Post this both numpy and pandas were not present and were installed fresh. No issues anymore.
like image 44
Harshit Nagar Avatar answered Mar 03 '26 02:03

Harshit Nagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!