Skip to content

Commit

Permalink
Simplify _tables_stats()
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Oct 21, 2024
1 parent b421caf commit 607909a
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions audbcards/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,16 @@ def tables_preview(self) -> typing.Dict[str, typing.List[typing.List[str]]]:
| 11 | 26 | male | deu |
"""
return {table: stats["preview"] for table, stats in self._tables_stats.items()}
preview = {}
for table, stats in self._tables_stats.items():
df = stats["preview"]
df = df.reset_index()
header = [df.columns.tolist()]
body = df.astype("string").values.tolist()
# Remove unwanted chars and limit length of each entry
body = [[self._parse_text(column) for column in row] for row in body]
preview[table] = header + body
return preview

@functools.cached_property
def tables_rows(self) -> typing.Dict[str, int]:
Expand Down Expand Up @@ -786,7 +795,7 @@ def _tables_stats(self) -> typing.Dict[str, dict]:
A dictionary with table names as keys and dictionaries containing:
- "columns": number of columns
- "rows": number of rows
- "preview": table preview (header + first 5 rows)
- "preview": dataframe preview (first 5 rows)
"""
stats = {}
Expand All @@ -797,25 +806,11 @@ def _tables_stats(self) -> typing.Dict[str, dict]:
version=self.version,
verbose=False,
)

columns = len(df.columns)

rows = len(df)

# Table preview
df = df.reset_index()
header = [df.columns.tolist()]
body = df.head(5).astype("string").values.tolist()
# Remove unwanted chars and limit length of each entry
body = [[self._parse_text(column) for column in row] for row in body]
preview = header + body

stats[table] = {
"columns": columns,
"rows": rows,
"preview": preview,
"columns": len(df.columns),
"rows": len(df),
"preview": df.head(5),
}

return stats

@staticmethod
Expand Down

0 comments on commit 607909a

Please sign in to comment.