How do I import Pandas on Apple M1 chip

Viewed 11102

When I call:

import pandas as pd

on tensorflow_macos (for M1 chip)

I get:

ImportError                               Traceback (most recent call last)
~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/__init__.py in <module>
     28 try:
---> 29     from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib
     30 except ImportError as e:  # pragma: no cover

~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/__init__.py in <module>
     12 
---> 13 from pandas._libs.interval import Interval
     14 from pandas._libs.tslibs import (

ImportError: dlopen(/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
<ipython-input-3-94f55571b0d6> in <module>
      1 import numpy as np
----> 2 import pandas as pd
      3 
      4 import matplotlib.pyplot as plt
      5 

~/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/__init__.py in <module>
     31     # hack but overkill to use re
     32     module = str(e).replace("cannot import name ", "")
---> 33     raise ImportError(
     34         f"C extension: {module} not built. If you want to import "
     35         "pandas from the source directory, you may need to run "

ImportError: C extension: dlopen(/Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/ashkan/tensorflow_macos_venv/lib/python3.8/site-packages/pandas/_libs/interval.cpython-38-darwin.so: mach-o, but wrong architecture not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --force' to build the C extensions first.
4 Answers

I hope Xcode Command Line Tools is already installed. If not, please install it then follow the steps.

Step 1:miniforge

Install miniforge for arm64 (Apple Silicon) from miniforge GitHub. Miniforge enables installing python packages natively compiled for Apple Silicon.

Step 2: create Conda environment

Don’t forget to open a new session or to source your .zshrc after miniforge install and before going through this step.

Create an empty Conda environment, then activate it and install python 3.8. and all the needed packages. Please note numpy is unnecessary here as pandas already install it, but it will be overwritten in the last step with the version provided by Apple.

conda create --name mytf
conda activate mytf
conda install -y python==3.8.6
conda install -y pandas matplotlib scikit-learn jupyterlab

I have tested it. It will work after these steps.

Follow these steps - Important With Tensorflow Installation

With miniforge installation do this -

conda install numpy scipy scikit-learn pandas matplotlib seaborn

Use Anaconda visual UI to install into the conda environment you created. You just select your environment and search for pandas.

Related