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
Metadata
Release files for polar-spark-df 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| polar_spark_df-0.1.0.tar.gz | 14.1 kB | Without provenance | Details for polar_spark_df-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| polar_spark_df-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for polar_spark_df-0.1.0-py3-none-any.whl |
Total release size: 23.7 kB
Release files / polar_spark_df-0.1.0.tar.gz
| Download URL | polar_spark_df-0.1.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5c03dee39e3582e9eb5de8c3f822c62d23f9066f99b06c22349a493de3e9a0cd
|
|
BLAKE2b-256 checksum How to use checksums |
a3530da46783f2980cd8ed400db6c9a42b3a4edb98705e5567f84c616fd7c348
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Release files / polar_spark_df-0.1.0-py3-none-any.whl
| Download URL | polar_spark_df-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c97e509e48e5d47c2beee8e8112ce6487a5175ca8578920368003f5a0fb55849
|
|
BLAKE2b-256 checksum How to use checksums |
0da4660cf745f1f5bb75e333fa002e5910d5418bd91c2b1e3f04381772ed8a13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Security for polar-spark-df 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.