Load the Layout object from the given dataframe. Args: df (pd.DataFrame): block_type (str): If there's no block_type column in the CSV file, you must pass in a block_type variable such that layout parser can appropriately detect the type of t
(df: pd.DataFrame, block_type: str = None)
| 111 | |
| 112 | |
| 113 | def load_dataframe(df: pd.DataFrame, block_type: str = None) -> Layout: |
| 114 | """Load the Layout object from the given dataframe. |
| 115 | |
| 116 | Args: |
| 117 | df (pd.DataFrame): |
| 118 | |
| 119 | block_type (str): |
| 120 | If there's no block_type column in the CSV file, |
| 121 | you must pass in a block_type variable such that layout parser |
| 122 | can appropriately detect the type of the layout elements. |
| 123 | |
| 124 | Returns: |
| 125 | Layout: |
| 126 | The parsed Layout object from the CSV file. |
| 127 | """ |
| 128 | df = df.copy() |
| 129 | if "points" in df.columns: |
| 130 | if df["points"].dtype == object: |
| 131 | df["points"] = df["points"].map( |
| 132 | lambda x: ast.literal_eval(x) if not pd.isna(x) else x |
| 133 | ) |
| 134 | |
| 135 | if block_type is None: |
| 136 | if "block_type" not in df.columns: |
| 137 | raise ValueError( |
| 138 | "`block_type` not specified both in dataframe and arguments" |
| 139 | ) |
| 140 | else: |
| 141 | df["block_type"] = block_type |
| 142 | |
| 143 | if any(col in TextBlock._features for col in df.columns): |
| 144 | # Automatically setting index for textblock |
| 145 | if "id" not in df.columns: |
| 146 | df["id"] = df.index |
| 147 | |
| 148 | return load_dict(df.apply(lambda x: x.dropna().to_dict(), axis=1).to_list()) |
no test coverage detected