sparkpolars
sparkpolars is a lightweight library designed for seamless conversions between Apache Spark and Polars without unnecessary dependencies. (Dependencies are only required when explicitly requested.)
Installation
pip install sparkpolars
# or
conda install skandev::sparkpolars
Requirements
- Python ≥ 3.10
- Apache Spark ≥ 3.3.0 (must be pre-installed)
- Polars ≥ 1.0 (must be pre-installed)
- Pyspark must also be installed if you plan to use this library
Why Does This Library Exist?
The Problem
Typical conversions between Spark and Polars often involve an intermediate Pandas step:
# Traditional approach:
# Spark -> Pandas -> Polars
# or
# Polars -> Pandas -> Spark
The Solution
sparkpolars eliminates unnecessary dependencies like pandas and pyarrow by leveraging native functions such as .collect() and schema interpretation.
Key Benefits
- 🚀 No extra dependencies – No need for Pandas or PyArrow
- ✅ Reliable handling of complex types – Provides better consistency for
MapType,StructType, and nestedArrayType, where existing conversion methods can be unreliable
Features
- Convert a Spark DataFrame to a Polars DataFrame or LazyFrame
- Ensures schema consistency: preserves
LongTypeasInt64instead of mistakenly converting toInt32 - Three conversion modes:
NATIVE,ARROW,PANDAS NATIVEmode properly convertsMapType,StructType, and nestedArrayTypeARROWandPANDASmodes may have limitations with complex types- Configurable conversion settings for Polars
list(struct)to SparkMapType - Timezone and time unit customization for Polars
Datetime
Usage
0. Supercharge Polars and Spark DataFrame
In your __init__.py file at the root project you can do the following for ease of use
from sparkpolars import toPolars, to_spark
from pyspark.sql import DataFrame as SparkDataFrame
from polars import DataFrame as PolarsDataFrame, LazyFrame as PolarsLazyFrame
__all__ = [
"toPolars",
"to_spark",
]
SparkDataFrame.toPolars = toPolars
PolarsDataFrame.to_spark = to_spark
PolarsLazyFrame.to_spark = to_spark
1. From Spark to Polars DataFrame
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("example").getOrCreate()
df = spark.createDataFrame([(1, 2)], ["a", "b"])
polars_df = df.toPolars()
2. From Spark to Polars LazyFrame
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("example").getOrCreate()
df = spark.createDataFrame([(1, 2)], ["a", "b"])
polars_df = df.toPolars(lazy=True)
3. From Polars DataFrame to Spark
from pyspark.sql import SparkSession
from polars import DataFrame
spark = SparkSession.builder.appName("example").getOrCreate()
df = DataFrame({"a": [1], "b": [2]}) # It can also be a LazyDataFrame
spark_df = df.to_spark(spark=spark)
# or
spark_df = df.to_spark() # It will try to get the Spark ActiveSession
4. Using Specific Mode
from sparkpolars import ModeMethod
spark_df = df.to_spark(mode=ModeMethod.NATIVE)
spark_df = df.to_spark(mode=ModeMethod.PANDAS)
spark_df = df.to_spark(mode=ModeMethod.ARROW)
polars_df = df.toPolars(mode=ModeMethod.NATIVE)
polars_df = df.toPolars(mode=ModeMethod.PANDAS)
polars_df = df.toPolars(mode=ModeMethod.ARROW)
5. Using Config
from sparkpolars import Config
conf = Config(
map_elements=["column_should_be_converted_to_map_type", ...], # Specify columns to convert to MapType
time_unit="ms", # Literal["ns", "us", "ms"], defaults to "us"
)
spark_df = df.to_spark(config=conf)
polars_df = df.toPolars(config=conf)
Known Limitations
JVM Timezone Discrepancy
Spark timestamps are collected via the JVM, which may differ from Spark’s timezone settings. If issues arise, verify the JVM timezone.
Memory Constraints
Collecting large datasets into memory can exceed available driver memory, leading to failures. (as for pandas/arrow)
Handling MapType:
From Spark to Polars
If you have in Spark:
Type: StructField("example", MapType(StringType(), IntegerType()))
Data: {"a": 1, "b": 2}
Then it will become in Polars:
Type: {"example": List(Struct("key": String, "value": Int32))}
Data: [{"key": "a", "value": 1}, {"key": "b", "value": 2}]
From Polars to Spark
If you have in Polars:
Type: {"example": List(Struct("key": String, "value": Int32))}
Data: [{"key": "a", "value": 1}, {"key": "b", "value": 2}]
Then it will become in Spark without specifying any config (Default Behavior):
Type: StructField("example", ArrayType(StructType(StructField("key", StringType())), StructField("value", IntegerType())))
Data: [{"key": "a", "value": 1}, {"key": "b", "value": 2}]
If you want this data to be converted to MapType:
from sparkpolars import Config
conf = Config(
map_elements=["example"]
)
Type: StructField("example", MapType(StringType(), IntegerType()))
Data: {"a": 1, "b": 2}
License
- pending
Contribution
- pending
Metadata
Release files for sparkpolars 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 |
|---|---|---|---|---|
| sparkpolars-0.1.0.tar.gz | 20.5 kB | Without provenance | Details for sparkpolars-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sparkpolars-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for sparkpolars-0.1.0-py3-none-any.whl |
Total release size: 32.2 kB
Release files / sparkpolars-0.1.0.tar.gz
| Download URL | sparkpolars-0.1.0.tar.gz |
|---|---|
| Size | 20.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ede578c6d9cc355cca5cb8e89c7bae0567c6eb183385af5a36da3debea1cb7f0
|
|
BLAKE2b-256 checksum How to use checksums |
a2e289b7ef4562de56143c7eea5fd224bae5f71838ade2bf2122091f4e55fd76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / sparkpolars-0.1.0-py3-none-any.whl
| Download URL | sparkpolars-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e9b5e1d77b337368ce927632c50f8679926c96b64047ddfd707ac6103834b35
|
|
BLAKE2b-256 checksum How to use checksums |
75a9a2437154adbe0038f1c6156d01d44e4d20e1440cb3a3cda11ee36e327849
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Security for sparkpolars 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.