Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import pandas on jupyter ipython notebook fails

I am able to import the pandas package within the spyder ide; however, if I attempt to open a new juypter notebook, the import fails.

I use the Anaconda package distribution on MAC OS X.

Here is what I do:

In [1]: import pandas 

and this is the response I get:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-97925edf8fb0> in <module>()
----> 1 import pandas

//anaconda/lib/python2.7/site-packages/pandas/__init__.py in <module>()
     11                       "pandas from the source directory, you may need to run "
     12                       "'python setup.py build_ext --inplace' to build the C "
---> 13                       "extensions first.".format(module))
     14 
     15 from datetime import datetime

ImportError: C extension: hashtable not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
like image 432
Pat Avatar asked Feb 07 '16 12:02

Pat


People also ask

Why does import pandas not work?

The most frequent source of this error is that you haven't installed Pandas explicitly with pip install pandas . Alternatively, you may have different Python versions on your computer, and Pandas is not installed for the particular version you're using.

How do I install pandas on IPython?

You can learn how to install pandas from their official website : http://pandas.pydata.org/. Best way to get pandas is to install via conda Builds for osx-64,linux-64,linux-32,win-64,win-32 for Python 2.7, Python 3.4, and Python 3.5 are all available.

Does Jupyter notebook support pandas?

In JupyterLab, create a new (Python 3) notebook: In the first cell of the notebook, you can import pandas and check the version with: Now you are ready to use pandas, and you can write your code in the next cells.


2 Answers

You have more than one Python 2 engines installed. One in your main OS platform, another one inside Anaconda's virtual environment. You need to install Panda on the latter.

Run in your Bash prompt:

which python

Then run the following in Jupyter/IPython and compare the result with the output you got from the Bash script:

from sys import executable
print(executable)

If they differ, you should note the result of the latter (i.e. copy it), and then go to your Bash prompt, and do as follows:

<the 2nd output> -m pip install pandas

so it would be something like this:

/usr/bin/anaconda/python2 -m pip install pandas

And Pandas will be installed for your Anaconda Python.

There is a way to add library paths to your existing environment, using sys.path.append('path to alternative locations'), but this has to be done every time your want to use the alternative environment as the effects are temporary.

You can alternatively install everything in your main environment:

python -m pip install cython scipy panda matplotlib jupyter notebook ipython

Update:

Based on responses to the above section:

Install homebrew like so:

In your Terminal:

xcode-select --install

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

then run:

brew doctor
brew update
brew upgrade

Now go ahead and install Python 2 through Brew:

brew install python

or for Python 3

brew install python3

Or do both. The install other useful stuff!

brew install git conda gfortran clang pkg-config

Then you can go ahead and install your desired libraries either using brew, or using pip, but first you should ensure that pip itself is installed.

easy_install pip

then you can install Python packages like so (NumPy is included in SciPy, and SciPy and Matplotlib depend on Cython and C, Scipy additionally uses fortran for ODE):

python2 -m install cython scipy pandas matplotlib jupyter

you can do that same thing for Python 3.

This clean install should really solve the problem. If it didn't, download Python from Python.org and re-install it. brew sometime refuses to install a package if it finds out that the package already exists. I don't recommend removing Python 2 so that you can install it through brew. That might cause issues with OS X. So the best alternative is to repair existing installations by installing the package downloaded from the website. OS X ensures that the package is installed in the right place. Once this is done, you can then go back to the instructions, but start from brew install python3.

like image 119
Pouria Avatar answered Oct 22 '22 04:10

Pouria


I had the same issue on Mac OS X with Anaconda (Python 2). I tried importing the pandas package in python repl, and got this error:

ValueError: unknown locale: UTF-8

Therefore, I've added the following lines to my ~/.bash_profile:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

And this has fixed the issue for me.

like image 32
volhv Avatar answered Oct 22 '22 03:10

volhv