Skip to main content

Facade to collect rows one-by-one into a Polars DataFrame (in the least-bad way)

Project description

polars-row-collector

PyPI Python License CI Docs Built for Polars

Facade to collect rows one-by-one into a Polars DataFrame (in the least-bad way)

Getting Started Example

Add the library to your dependencies: uv add polars_row_collector

import polars as pl
from polars_row_collector import PolarsRowCollector

collector = PolarsRowCollector(
    # Note: Schema is optional, but recommended.
    schema={"col1": pl.Int64, "col2": pl.Float64}
)

for item in items:
    row = {
        "col1": item.value1,
        "col2": item.value2,
    }
    collector.add_row(row)

df = collector.to_df()

You can think of collector as filling the same niche as a list_of_dfs: list[pl.DataFrame].

Features

  • Highly performant and memory-optimized.
    • Much more-so than collecting into a list[dict[str, Any]] or concatenating one-row dataframes.
  • Optionally supply a schema for the incoming rows.
  • Thread-safe (when GIL is enabled - default in Python <= 3.15).
  • Configuration arguments for safety vs. performance tradeoffs:
    • Behaviour if there are missing columns: Enforce all columns present or allow missing columns.
    • Behaviour if there are extra columns: Drop silently or raise.
    • Maintain insertion order.

Example Applications

  • Gathering data in a web scraping/parsing tool.
  • Gathering/batching incoming log messages or event logs before writing in bulk to some destination.
  • Gathering data in a document parsing pipeline (e.g., XML with lots of conditionals).

Future Features

  • Intermediate to-disk storage to temporary parquet files to larger-than-memory collections.
  • Further optimize appending many rows at once.
  • Read the dataframe so-far, in the middle of gathering rows.
  • Documentation.

Disclaimer

As the project's description says, this is the "least-bad way" to accomplish this pattern.

If you can implement your code in such a way that you're not collecting individual rows of a dataframe, you are likely better-off doing it that way (e.g., collecting a list[pl.DataFrame]).

However, there are always exceptions to the best practices, and this library is significantly more efficient (performance and memory) than collecting into a list[dict[str, Any]].

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_row_collector-0.2.1.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polars_row_collector-0.2.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file polars_row_collector-0.2.1.tar.gz.

File metadata

  • Download URL: polars_row_collector-0.2.1.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for polars_row_collector-0.2.1.tar.gz
Algorithm Hash digest
SHA256 618ac0ac3bcaa4794d58650538d9a9a4d3665e8261ca8ffda5fb7f134dca9f9a
MD5 e342481afc5991c041fe864cb3974ac6
BLAKE2b-256 dffb32f541eff4eccb30837d3eb72c913aa9b21a9fc0eb141574d3db50356cf0

See more details on using hashes here.

File details

Details for the file polars_row_collector-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: polars_row_collector-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for polars_row_collector-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93050827d8e6743c60b6cb778fbb4fe54c949237e783a8200752a0751c416e99
MD5 19d91c306f8482631f8235694db9ad3b
BLAKE2b-256 eb7b50031a9bc14ae84caad836c50d7f07f27fb1aa910553553bebf3f9daf548

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page