Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 854
Stephina Pascho Avatar asked Sep 14 '25 04:09

Stephina Pascho


1 Answers

It's not necessary to import numpy before importing pandas. numpy is implicitly imported by and internally used within the pandas module. 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

In [4]: s.mean()
Out[4]: np.float64(4.5)

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.

like image 84
user545424 Avatar answered Sep 15 '25 17:09

user545424