File size: 878 Bytes
c289d87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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
|