rawwar
rawwar

Reputation: 5012

how Dataframe class is imported directly from pandas package

in pandas we can directly do the following

import pandas as pd

df = pd.DataFrame()

here, pandas is a package. DataFrame is a class. So, how does this work because, DataFrame was actually defined in a pandas.core.frame ( it is defined in frame.py which is inside core folder of pandas.)

Note: i was thinking that this kind of behavior can be achieved by doing something in __init__.py file. Can anyone help me understand this.

Upvotes: 2

Views: 1827

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

__init__.py is technically just another python module, so any name defined in a package's __init__.py is directly accessible from the package itself. And it's indeed a common pattern in Python to use a package's __init__.py as a facade for submodules / subpackages.

FWIW note that panda's __init__.py, while an example of using __init__.py as a facade, is not following good practices in that it uses "star imports" (from submodule import *) which makes it very painful to trace the origin of a name (the module where it is defined) - specially with such a large package as Panda - and is very brittle too since if two submodules export a same name the one imported last will shadow the first one. The good practice is to always explicitely specify which names you want to import:

from submodule1 import foo, bar
from submodule2 import baaz, quux

which makes clear where a name comes from and will make a duplicate name much more obvious:

from submodule1 import foo, bar
from submodule2 import baaz, quux
from submodule3 import foo # oops, we will have to rename either this one or submodule1.foo

Upvotes: 3

dylan_fan
dylan_fan

Reputation: 720

Dataframe as you said is defined in pandas/core/frame.py.

Let's take a look at pandas/__init__.py in pandas directory on github.

Line 42:

from pandas.core.api import *

And pandas/core/api.py imports Dataframe from pandas/core/frame.py in line 23:

from pandas.core.frame import DataFrame

So since you import * from pandas/core/api.py in pandas/__init__.py and pandas/core/api.py imports Dataframe then you have Dataframe imported directly to pandas.

Upvotes: 2

Related Questions