Skip to content

Private method: _create_df

ScanPyImports.analyzer.DataAnalyzer._create_df()

Create a DataFrame with the data on imported modules.

Returns:

  • Optional[DataFrame]

    DataFrame with import data or None if the directory does not exist.

Source code in ScanPyImports/analyzer.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def _create_df(self) -> Optional[pd.DataFrame]:
    """
    Create a DataFrame with the data on imported modules.

    Returns:
        DataFrame with import data or None if the directory does not exist.
    """
    if not self.directory.exists:
        return None

    data_frames = []
    for file in self.directory.files:
        if not file.has_imports:
            continue

        file_data_frames = []
        for line in file.lines:
            line_df = pd.DataFrame(line.imports)
            line_df["alias"] = line.alias
            # line_df["alias"] = pd.Series(line.alias)
            line_df["original"] = line.original

            file_data_frames.append(line_df)

        file_df = pd.concat(file_data_frames, ignore_index=True)
        file_df['path'] = file.file
        file_df['extension'] = file.extension
        file_df['file'] = os.path.basename(file.file)
        file_df['filename'] = os.path.basename(file.file).split('.')[0]
        file_df['directory'] = os.path.dirname(file.file)
        data_frames.append(file_df)

    if not data_frames:
        return None

    directory_df = pd.concat(data_frames, ignore_index=True)

    cols = list(directory_df.columns)
    cols.sort( key= lambda c: str(c) )
    directory_df = directory_df[cols].rename(columns=lambda col: 
                                       f'imported_{col}' 
                                       if isinstance(col, int) else col)

    return directory_df