import argparse import tempfile import httpx import polars as pl from pathlib import Path from tqdm import tqdm URL = "http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-complete.csv" def download_with_progress(url: str, output_path: Path) -> None: with httpx.stream( "GET", url, follow_redirects=True, timeout=httpx.Timeout(30.0, read=None), ) as response: response.raise_for_status() # pyright: ignore[reportUnusedCallResult] total = int(response.headers.get("content-length", 0)) with ( open(output_path, "wb") as f, tqdm( total=total, unit="B", unit_scale=True, unit_divisor=1024, desc="Downloading", ) as pbar, ): for chunk in response.iter_bytes(chunk_size=8192): f.write(chunk) pbar.update(len(chunk)) return def convert_to_parquet(csv_path: Path, parquet_path: Path) -> None: """Convert CSV to Parquet using Polars.""" print("Converting to Parquet...") # https://www.gov.uk/guidance/about-the-price-paid-data # Land Registry CSV columns columns = [ "transaction_id", "price", "date_of_transfer", "postcode", "property_type", "old_new", "duration", "paon", "saon", "street", "locality", "town_city", "district", "county", "ppd_category", "record_status", ] df = pl.read_csv( csv_path, has_header=False, new_columns=columns, try_parse_dates=True, ) parquet_path.parent.mkdir(parents=True, exist_ok=True) print(f"Columns: {df.collect_schema().names()}") df.write_parquet(parquet_path, compression="zstd") print(f"Saved to {parquet_path}") def main() -> None: parser = argparse.ArgumentParser(description="Download and convert Land Registry price-paid data") parser.add_argument("--output", type=Path, required=True, help="Output parquet file path") args = parser.parse_args() with tempfile.TemporaryDirectory() as cache_dir: csv_path = Path(cache_dir) / "price-paid-complete.csv" download_with_progress(URL, csv_path) convert_to_parquet(csv_path, args.output) if __name__ == "__main__": main()