| from __future__ import annotations | |
| from pathlib import Path | |
| import pandas as pd | |
| def read_table(path: str | Path) -> pd.DataFrame: | |
| source = Path(path) | |
| suffix = source.suffix.lower() | |
| if suffix in {".csv", ".txt", ".tsv"}: | |
| sep = "\t" if suffix in {".tsv", ".txt"} else "," | |
| return pd.read_csv(source, sep=sep) | |
| if suffix == ".parquet": | |
| return pd.read_parquet(source) | |
| raise ValueError(f"Unsupported table format: {source}") | |
| def write_table(df: pd.DataFrame, path: str | Path) -> Path: | |
| target = Path(path) | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| suffix = target.suffix.lower() | |
| if suffix == ".csv": | |
| df.to_csv(target, index=False) | |
| elif suffix == ".parquet": | |
| df.to_parquet(target, index=False) | |
| else: | |
| raise ValueError(f"Unsupported output table format: {target}") | |
| return target | |