ValueError: numpy.dtype has the wrong size, try recompiling
Asked Answered
T

13

89

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
Tungstic answered 17/7, 2013 at 20:30 Comment(6)
How did you install pandas?Balfour
possible duplicate of Cannot import Scikit-LearnDiatribe
Did you try recompiling?Eveevection
Why not use the python(x,y), enthought or winpython distribution? They are specifically designed to eliminate these compilation and installation problems for scientific python users under windows and come with most packages you are likely to want precompiled.Geithner
This message shows up when pandas is compiled against a newer numpy version than the one you have installed. You either need to recompile pandas against the numpy version that you have installed or update numpy.Iridescent
@Geithner Because sometimes you need to work with a specific Python distribution shipped with an another program (ArcGIS, for example).Hastings
I
69

(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.

Iridescent answered 21/8, 2013 at 23:32 Comment(1)
I tried updating numpy and pandas and I still get this error, Any ideas?Leavenworth
K
37

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
Kimbro answered 17/1, 2015 at 12:57 Comment(1)
I just executed "easy_install --upgrade numpy", but didn't install up-to-date related packages like pandas andk scikit-learn. Everything is ok too.Premeditation
W
33

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
Warmongering answered 18/2, 2016 at 8:42 Comment(2)
With 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.Baro
@Roy- This depends on if the system python is being used or the brew version afaik- sudo should not be needed with brew python.Warmongering
N
5

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.

Nonsectarian answered 23/8, 2013 at 12:19 Comment(0)
R
2

I also encounter this error when use pandas to access MYSQL. This error message indicates a binary compatible issue and can be resolved by using latest version of pandas and numpy package. Here is my steps to resolve this issue, and it works well on my Ubuntu 12.04:

cd /tmp/
wget https://pypi.python.org/packages/source/p/pandas/pandas-0.12.0.tar.gz
tar xzvf pandas-0.12.0.tar.gz
cd pandas-0.12.0
easy_install --upgrade numpy
Ruy answered 29/10, 2013 at 14:13 Comment(1)
I 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 programBurgwell
N
2

As in here, for me only sudo pip install pandas==0.13.1 worked

Neves answered 29/2, 2016 at 8:15 Comment(0)
M
1

In my case, I had installed pandas-0.10.0.win-amd64-py2.7 but was checking to see if a bug had been fixed in a more recent version of pandas. So I did an easy_install -U to force the upgrade, but then got the above error due to some incompatibilities with numpy etc... when I did

import pandas

To fix, I just reinstalled the pandas-0.10.0.win-amd64-py2.7 binary and everything works. I didn't see this answer (suggests to use pip) which may have helped me (though not sure) Install particular version with easy_install

Also this highlights why one should use virtualenv (which I wasn't).

Madore answered 4/2, 2014 at 15:13 Comment(0)
B
1

For me (Mac OS X Mavericks) it worked to install the version for python2.6:

sudo port install py26-scikit-learn

then run:

python2.6 myscript.py
Brotherton answered 6/8, 2014 at 15:54 Comment(0)
C
1

The problem I solved on Webfaction was old numpy library(1.5) which was in conflict with my fresh

pip install pandas

installation in .virtualenv.

The problem was solved after I did pip install pandas out of the virtual environment. The idea came from discussion on https://github.com/pydata/pandas/issues/3711, thanks, cpcloud!

Ciracirca answered 11/8, 2014 at 23:36 Comment(0)
K
1

Like @user333700 said, required versions of libraries may not meet for each other. You get one library as another's dependency. Then without knowing it was already installed as dependency, you need that specific library and you install one version. With such ways dependencies may mess up.

I lived such a case and looked for a solution. Found this: https://mcmap.net/q/242706/-how-to-fix-python-numpy-pandas-installation

I had two different versions for egg-info file and folder name of numpy:

drwxr-xr-x. 19 root root   4096 Sep 25 15:00 numpy
drwxr-xr-x.  2 root root   4096 Sep 22 11:25 numpy-1.13.1.dist-info
-rw-r--r--.  1 root root   1630 Nov 20  2015 numpy-1.7.1-py2.7.egg-info

I removed them all and reinstalled numpy with pip.

Koffman answered 25/9, 2017 at 15:45 Comment(0)
H
0

I just meet this 'ValueError' issue and have addressed it. Definitely there's something wrong with numpy package.

But when I try to pip install --upgrade numpy it failed, so I uninstall and download the newest numpy.zip file. Then manually uncompress and python setup.py install it.

Luckly, it works!

Hardened answered 29/12, 2015 at 9:58 Comment(1)
It failed for me also. Then I tried sudo pip install --upgrade numpyand it worked.Alyssa
S
0

I had a similar issue, and simply re-installing using pip install ... as suggested in previous comments didn't work.

What worked for me was re-installing with the added flag pip install --no-cache-dir ..., seems there was an incompatible numpy version somewhere in the cache.

Sarrusophone answered 31/12, 2018 at 18:42 Comment(0)
G
-1

There are cases where you want to keep a specific NumPy version and the upgrade option mentioned here will not work. An example that occurred to me was the Python distribution preinstalled with ArcGIS. For ArcPy to work in ArcGIS 10.5.1, that distribution needs to be Python 2.7.12 with NumPy 1.9.3 and any other version of NumPy is probably going to cause issues with your ArcPy functionality.

What you can do with this case is try to install a specific, older version of the problematic third-party library that is supposed to be compatible with the older NumPy version that ArcGIS has.

For instance, scikit-learn 0.19.1 would NOT operate with NumPy 1.9.3 and would result in the same error you mentioned. However, scikit-learn 0.15 works fine. You can test different versions to find the one that works. Just mention the version number through pip:

python -m pip install scikit-learn==0.15
Gagliano answered 24/5, 2018 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.