High-performance converters between PySpark and Polars DataFrames
Project description
Polar-Spark-DF
High-performance converters between PySpark and Polars DataFrames with optimized memory usage.
Features
- 🚀 High Performance: Optimized for speed and memory efficiency
- 🔄 Bidirectional Conversion: Convert from PySpark to Polars and vice versa
- 🧱 Builder Pattern: Fluent interface for easy configuration
- 📊 Batched Processing: Handle large datasets with configurable batch sizes
- 🏹 Apache Arrow Support: Use Arrow for even faster conversions when available
- 📈 Benchmarking Tools: Built-in utilities to measure performance
- 🧪 Well Tested: Comprehensive test suite ensures reliability
- 🔄 Type Safety: Robust handling of various data types
Installation
pip install polar-spark-df
Quick Start
Converting PySpark DataFrame to Polars
from polar_spark_df import DataFrameConverter
# Convert PySpark DataFrame to Polars
polars_df = (
DataFrameConverter()
.with_spark_df(spark_df)
.with_batch_size(10000)
.with_use_arrow(True)
.to_polars()
)
Converting Polars DataFrame to PySpark
from polar_spark_df import DataFrameConverter
# Convert Polars DataFrame to PySpark
spark_df = (
DataFrameConverter()
.with_polars_df(polars_df)
.with_spark_session(spark)
.with_schema(schema) # Optional
.with_use_arrow(True)
.to_spark()
)
Configuration Options
The DataFrameConverter class provides several configuration options:
| Method | Description |
|---|---|
with_spark_df(spark_df) |
Set the PySpark DataFrame to convert |
with_polars_df(polars_df) |
Set the Polars DataFrame to convert |
with_spark_session(spark_session) |
Set the SparkSession to use |
with_schema(schema) |
Set the schema for PySpark DataFrame creation |
with_batch_size(batch_size) |
Set the batch size for processing (default: 100000) |
with_use_arrow(use_arrow) |
Whether to use Arrow for conversion (default: True) |
with_preserve_index(preserve_index) |
Whether to preserve the index (default: False) |
with_optimize_string_conversion(optimize) |
Whether to optimize string conversion (default: True) |
with_type_mapping(type_mapping) |
Set custom type mapping for conversion |
Performance Optimization
Batch Size
The batch size controls how many rows are processed at once. A larger batch size may improve performance but requires more memory.
converter = (
DataFrameConverter()
.with_batch_size(50000) # Process 50,000 rows at a time
# ... other configuration
)
Apache Arrow
Using Apache Arrow can significantly improve performance, especially for large datasets:
converter = (
DataFrameConverter()
.with_use_arrow(True) # Use Arrow for conversion (default)
# ... other configuration
)
Supported Data Types
The converter supports a wide range of data types:
| Category | PySpark Types | Polars Types |
|---|---|---|
| Numeric | IntegerType, LongType, FloatType, DoubleType, DecimalType |
Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64, Decimal |
| String | StringType |
Utf8 |
| Boolean | BooleanType |
Boolean |
| Temporal | DateType, TimestampType |
Date, Datetime, Time |
| Complex | ArrayType, StructType, MapType |
List, Struct |
Benchmarking
The package includes benchmarking utilities to help you find the optimal configuration for your data:
from polar_spark_df.benchmark import benchmark_spark_to_polars, print_benchmark_results
# Run benchmarks with different configurations
results = benchmark_spark_to_polars(
spark_df,
batch_sizes=[10000, 50000, 100000],
use_arrow=[True, False]
)
# Print results
print_benchmark_results(results)
Benchmark Results
Benchmark run on 2025-05-23T12:45:00.000000
Dataset: 100000 rows × 10 columns
PySpark to Polars Conversion
| Configuration | Time (s) | Memory (MB) |
|---|---|---|
| arrow=True,batch_size=10000 | 0.4521 | 125.45 |
| arrow=True,batch_size=50000 | 0.3876 | 245.32 |
| arrow=True,batch_size=100000 | 0.3654 | 412.78 |
| arrow=False,batch_size=10000 | 1.2345 | 98.76 |
| arrow=False,batch_size=50000 | 0.9876 | 187.65 |
| arrow=False,batch_size=100000 | 0.8765 | 356.43 |
Polars to PySpark Conversion
| Configuration | Time (s) | Memory (MB) |
|---|---|---|
| arrow=True,batch_size=10000 | 0.5432 | 145.67 |
| arrow=True,batch_size=50000 | 0.4321 | 267.89 |
| arrow=True,batch_size=100000 | 0.3987 | 432.10 |
| arrow=False,batch_size=10000 | 1.3456 | 112.34 |
| arrow=False,batch_size=50000 | 1.0987 | 198.76 |
| arrow=False,batch_size=100000 | 0.9876 | 378.90 |
Best Configurations
- PySpark to Polars: arrow=True,batch_size=100000 - 0.3654s, 412.78 MB
- Polars to PySpark: arrow=True,batch_size=100000 - 0.3987s, 432.10 MB
Performance Recommendations
Based on our benchmarks:
- Use Apache Arrow: Arrow-based conversion is consistently 2-3x faster than non-Arrow methods
- Batch Size Tradeoff: Larger batch sizes improve performance but increase memory usage
- Optimal Configuration: For most use cases,
arrow=Truewithbatch_size=50000provides a good balance of speed and memory usage - Memory Considerations: For memory-constrained environments, use smaller batch sizes with Arrow enabled
Examples
See the examples directory for more detailed usage examples:
Running Tests
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run benchmarks
pytest tests/test_benchmark.py -v
# Run data type tests
pytest tests/test_datatypes.py -v
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polar_spark_df-0.1.0.tar.gz.
File metadata
- Download URL: polar_spark_df-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c03dee39e3582e9eb5de8c3f822c62d23f9066f99b06c22349a493de3e9a0cd
|
|
| MD5 |
8fb0e4b13c5584a8f8ca1ffdd8958517
|
|
| BLAKE2b-256 |
a3530da46783f2980cd8ed400db6c9a42b3a4edb98705e5567f84c616fd7c348
|
File details
Details for the file polar_spark_df-0.1.0-py3-none-any.whl.
File metadata
- Download URL: polar_spark_df-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c97e509e48e5d47c2beee8e8112ce6487a5175ca8578920368003f5a0fb55849
|
|
| MD5 |
7aee083aa820705a5ed05551d0f9e37d
|
|
| BLAKE2b-256 |
0da4660cf745f1f5bb75e333fa002e5910d5418bd91c2b1e3f04381772ed8a13
|