MCPcopy Create free account
hub / github.com/Layout-Parser/layout-parser / load_dataframe

Function load_dataframe

src/layoutparser/io/basic.py:113–148  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

111
112
113def 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())

Callers 3

extract_words_for_pageFunction · 0.85
load_csvFunction · 0.85
gather_dataMethod · 0.85

Calls 3

load_dictFunction · 0.85
copyMethod · 0.80
to_dictMethod · 0.45

Tested by

no test coverage detected