pythonpandasnumpy

Do I need to import NumPy before importing pandas or can I use pandas by itself?


I know pandas is built on NumPy, and my class examples also always include import NumPy first. I'm just not sure if this is a required step or a "just in case" type situation.


Solution

  • It's not necessary to import numpy before importing pandas. For example:

    In [1]: import pandas as pd
    
    In [2]: s = pd.Series(range(10))
    
    In [3]: s
    Out[3]: 
    0    0
    1    1
    2    2
    3    3
    4    4
    5    5
    6    6
    7    7
    8    8
    9    9
    dtype: int64
    

    The reason that it is often imported along with pandas is that you often will create an array using numpy which is then passed to pandas.