This guide demonstrates how to read existing Parquet files, rename columns to a capitalized format using the polars library, and write the transformed data back to a new file path. This pattern is useful for preparing data structures before defining external tables in dbt.
To perform this transformation:
- Read the source Parquet file using
pl.read_parquet(). - Use
.rename() with a dictionary mapping old column names to new column names. - Ensure the destination directory exists using
os.makedirs(). - Write the resulting DataFrame to the new path using
.write_parquet().
import polars as pl
import os
# Define column mapping
new__col_dict = {'id':'Id', 'first_name':'First_Name', 'last_name':'Last_Name', 'email':'Email'}
fpath = 'integration_tests/public_data/parquet{}/section={}/people_{}.parquet'
# Prepare destination
dest_path = fpath.format('_capitalized', 'a', 'a')
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
# Read, transform, and write
df_a = pl.read_parquet(fpath.format('', 'a', 'a')).rename(new__col_dict)
df_a.write_parquet(dest_path)