ValueError: numpy.dtype has the wrong size, try recompiling
Solution 1
(to expand a bit on my comment)
Numpy developers follow in general a policy of keeping a backward compatible binary interface (ABI). However, the ABI is not forward compatible.
What that means:
A package, that uses numpy in a compiled extension, is compiled against a specific version of numpy. Future version of numpy will be compatible with the compiled extension of the package (for exception see below). Distributers of those other packages do not need to recompile their package against a newer versions of numpy and users do not need to update these other packages, when users update to a newer version of numpy.
However, this does not go in the other direction. If a package is compiled against a specific numpy version, say 1.7, then there is no guarantee that the binaries of that package will work with older numpy versions, say 1.6, and very often or most of the time they will not.
The binary distribution of packages like pandas and statsmodels, that are compiled against a recent version of numpy, will not work when an older version of numpy is installed. Some packages, for example matplotlib, if I remember correctly, compile their extensions against the oldest numpy version that they support. In this case, users with the same old or any more recent version of numpy can use those binaries.
The error message in the question is a typical result of binary incompatibilities.
The solution is to get a binary compatible version, either by updating numpy to at least the version against which pandas or statsmodels were compiled, or to recompile pandas and statsmodels against the older version of numpy that is already installed.
Breaking the ABI backward compatibility:
Sometimes improvements or refactorings in numpy break ABI backward compatibility. This happened (unintentionally) with numpy 1.4.0. As a consequence, users that updated numpy to 1.4.0, had binary incompatibilities with all other compiled packages, that were compiled against a previous version of numpy. This requires that all packages with binary extensions that use numpy have to be recompiled to work with the ABI incompatible version.
Solution 2
For me (Mac OS X Maverics, Python 2.7)
easy_install --upgrade numpy
helped. After this you can install up-to-date packages pandas, scikit-learn, e.t.c. using pip:
pip install pandas
Solution 3
I found it to be a simple version being outdated or mismatch and was fixed with:
pip install --upgrade numpy
pip install --upgrade scipy
pip install --upgrade pandas
Or might work with the one liner:
pip install --upgrade numpy scipy pandas
Solution 4
I had a similar error with another library and realized that I had several versions of numpy installed on my system. The fix for me was to edit my PYTHONPATH and put the site-packages that contained the latest version of numpy in first position.
Solution 5
As in here, for me only sudo pip install pandas==0.13.1
worked
Admin
Updated on January 01, 2020Comments
-
Admin over 4 years
I just installed pandas and statsmodels package on my python 2.7 When I tried "import pandas as pd", this error message comes out. Can anyone help? Thanks!!!
numpy.dtype has the wrong size, try recompiling Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\__init__.py", line 4, in <module> from formulatools import handle_formula_data File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\formulatools.p y", line 1, in <module> import statsmodels.tools.data as data_util File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\__init__.py", li ne 1, in <module> from tools import add_constant, categorical File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\tools.py", line 14, in <module> from pandas import DataFrame File "C:\analytics\ext\python27\lib\site-packages\pandas\__init__.py", line 6, in <module> from . import hashtable, tslib, lib File "numpy.pxd", line 157, in init pandas.tslib (pandas\tslib.c:49133) ValueError: numpy.dtype has the wrong size, try recompiling
-
xjzhou about 9 yearsI just executed "easy_install --upgrade numpy", but didn't install up-to-date related packages like pandas andk scikit-learn. Everything is ok too.
-
Henry Chinner about 8 yearsWorked for me too ( Ubuntu 14.04, Python 2.7 )
-
Harsh Wardhan almost 8 yearsIt failed for me also. Then I tried
sudo pip install --upgrade numpy
and it worked. -
Mona Jalal almost 8 yearsI did this and got this error
File "numpy/core/setup.py", line 654, in get_mathlib_info RuntimeError: Broken toolchain: cannot link a simple C program
-
Roy Iacob almost 8 yearsWith el capitan changes on OSX, I recommend using --user as an additional argument to prevent pip from overwriting system files. Users will otherwise feel tempted to use sudo.
-
RexFuzzle almost 8 years@Roy- This depends on if the system python is being used or the brew version afaik- sudo should not be needed with brew python.
-
NotSoShabby over 5 yearsI tried updating numpy and pandas and I still get this error, Any ideas?