Creating dataframes
Creating Dataframes
You can easily create example DataFrames from lists of model instances. This is particularly
useful for building test fixtures for a transformation. Pass the desired frame class to select
its backend. Spark also requires its session through the session keyword:
from dataclasses import dataclass
import polars as pl
from data_joinery import Schema
@dataclass
class Customer:
customer_id: str
name: str
is_active: bool
rows = [
Customer("1", "Alice", True),
Customer("2", "Bob", False),
Customer("3", "Charlie", True),
]
schema = Schema(Customer)
df = schema.create_dataframe(rows, pl.LazyFrame)
print(df.collect())
Outputs:
from dataclasses import dataclass
from pyspark.sql import DataFrame, SparkSession
from data_joinery import Schema
@dataclass
class Customer:
customer_id: str
name: str
is_active: bool
spark = SparkSession.builder.getOrCreate()
rows = [
Customer("1", "Alice", True),
Customer("2", "Bob", False),
Customer("3", "Charlie", True),
]
schema = Schema(Customer)
df = schema.create_dataframe(rows, DataFrame, session=spark)
df.show()
Outputs: