Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which file is a specified dataframe's attribution definition, such as columns, located?

pandas is a huge library in python.

import pandas as pd
pd.__path__
['/usr/local/lib/python3.5/dist-packages/pandas']  

I know the pandas library located in /usr/local/lib/python3.5/dist-packages/pandas.

data = {'Name':['Tom', 'nick'], 'Age':[20, 21]} 
df = pd.DataFrame(data) 
df.cloumns
Index(['Age', 'Name'], dtype='object')

columns is an attribution of dataframe, i want to know where is the dataframe's attribution columns defination?

ls  /usr/local/lib/python3.5/dist-packages/pandas
api     conftest.py  __init__.py  plotting     tests    _version.py
arrays  core         io           __pycache__  tseries
compat  errors       _libs        testing.py   util

In which directory and which file in the directory does the columns attribution locate? df.cloumns.__path__ can't give the answer.

like image 484
showkey Avatar asked Apr 05 '20 00:04

showkey


Video Answer


1 Answers

>>> import pandas as pd
>>> import inspect
>>> inspect.getfile(pd.DataFrame)
'/Users/.../lib/python3.7/site-packages/pandas/core/frame.py'

DataFrames would be initialized via __init__:
https://github.com/pandas-dev/pandas/blob/v1.0.3/pandas/core/frame.py#L414

Specifically, when constucting a DataFrame from a dict, it uses the @classmethod to instantiate the DF:
https://github.com/pandas-dev/pandas/blob/v1.0.3/pandas/core/frame.py#L1169

@classmethod
def from_dict(cls, data, orient="columns", dtype=None, columns=None) - "DataFrame":
    ...
    return cls(data, index=index, columns=columns, dtype=dtype)

Checked that file in github and think this is where the columns attribute is set:
https://github.com/pandas-dev/pandas/blob/v1.0.3/pandas/core/frame.py#L8449

DataFrame._setup_axes(
    ["index", "columns"],
    docs={
        "index": "The index (row labels) of the DataFrame.",
        "columns": "The column labels of the DataFrame.",
    },
)

EDIT: Added reference to def __init__, def from_dict and changed paths to stable pandas version

like image 171
the-ucalegon Avatar answered Oct 15 '22 00:10

the-ucalegon