Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

G-Zeus

is a package that chunk-reads GZipped text files LIGHTENING fast.

What is this package for?

This package is designed for workloads that

  1. Need to read data from a very large .csv.gz file

  2. You have additional rules that you want to apply while reading, and you want to work in a chunk by chunk fashion that saves you memory.

  3. You know what you are doing and prefer a more customizable experience than a package like polars_streaming_csv_decompression

This package provides a Chunker class that will read gz compressed text file by chunks. In the case of csv files, each chunk will represent a proper decompressed csv file and only the first chunk will have header info, if headers are present. The Chunker will produce these chunks in a streaming fashion, thus minimizing memory load.

This package can potentially be used to stream large gzipped text files as well. But is not capable of semantic chunking, which is often needed for text processing for LLMs. This package only chunks by identifying the last needle (new line character) in the haystack (text string) in the current buffer.

Assumptions

The new_line_symbol provided by the user only shows up in the underlying text file as new line symbol.

We get decompressed bytes by chunks, then what?

Most of the times, we only need to extract partial data from large .csv.gz files. This is where the combination of GZeus and Polars really shines.

If you have Polars installed already:

from gzeus import stream_polars_csv_gz

for output_of_your_func in stream_polars_csv_gz("PATH TO YOUR DATA", func = your_func):
    # do work on the output of your func

where your_func should be pl.LazyFrame -> Any. If you need more control over the iteration and data bytes, you can structure you code as below:

from gzeus import Chunker
import polars as pl

# Turn portion of the produced bytes into a DataFrame.
def bytes_into_df(df:pl.LazyFrame) -> pl.DataFrame:
    return df.filter(
        pl.col("City_Category") == 'A'
    ).select("City_Category", "Primary_Bank_Type", "Source").collect()

ck = (
    Chunker(buffer_size=1_000_000, new_line_symbol='\n')
    .with_local_file("../data/test.csv.gz")
)

df_temp = pl.scan_csv(ck.read_one()) # first chunk
schema = df_temp.collect_schema() # Infer schema from first chunk
dfs = [bytes_into_df(df_temp)]

dfs.extend(
    bytes_into_df(
        pl.scan_csv(byte_chunk, has_header=False, schema=schema)
    )
    for byte_chunk in ck.chunks()
)

df = pl.concat(dfs)
df.head()

Performance vs. Pandas

See here.

It is extremely hard to have an apples-to-apples comparison with other tools. Here I will focus on the comparison with pandas.read_csv, which has an iterator option. Note: GZeus chunks are defined by byte-sizes, while pandas.read_csv iterator has a fixed number of rows per chunk.

However, generally speaking, I find that for .csv.gz files:

  1. GZeus + Polars is at least a 50% reduction in time than pd.read_csv with zero additional work on each chunk
  2. If you set higher buffer size, GZeus + Polars can take only 1/5 of the time of pandas.read_csv.
  3. Even faster with more workload per chunk (mostly because of Polars).

Cloud Files

To support "chunk read" from any major cloud provider is no easy task. Not only will it require an async interface in Rust, which is much harder to write and maintain, but there are also performance issues related to getting only a small chunk of data each time. To name a few:

  1. Increase the number of calls to the storage
  2. Repeatedly opening the file and seeking to the last read position.
  3. Rate limits issues, especially with VPN. E.g. to get better performance, gzeus needs to read 10MB+ per chunk, but this will increase "packets per second" significantly.

A workaround is to use temp files. For example, for AWS s3, one can do the following:

import tempfile
import boto3

s3 = boto3.client('s3')

tmp = tempfile.NamedTemporaryFile()
s3.download_fileobj('amzn-s3-demo-bucket', 'OBJECT_NAME', tmp)
df = chunk_load_data_using_gzeus(tmp.name) # a wrapper function for the code shown above.
tmp.close()

Almost always, the machine should have enough disk space. In chunk_load_data_using_gzeus, data is read by chunks and therefore won't lead to OOM errors. It can be any wrapper around stream_polars_csv_gz provided by the package.

Other Projects to Check Out

  1. Dataframe-friendly data analysis package polars_ds
  2. For a more sophisticated but more feature-complete package for streaming csv.gz, see polars_streaming_csv_decompression

Release files for gzeus 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gzeus 0.1.2
File Size Uploaded
gzeus-0.1.2.tar.gz 16.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for gzeus 0.1.2
File
gzeus-0.1.2-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
gzeus-0.1.2-cp39-abi3-manylinux_2_24_aarch64.whl CPython 3.9 abi3 Linux glibc 2.24+ ARM64 Details
gzeus-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
gzeus-0.1.2-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
gzeus-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 1.2 MB

Release files / gzeus-0.1.2.tar.gz

Download URL gzeus-0.1.2.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5a0bf8309d72efc35b882a7ea055aaa891d196d244912406dfb19278e0ad1657
BLAKE2b-256 checksum
How to use checksums
394fdb12dff2cd9768747aae4c5391db4923c1b8ccb60ed8bfede7c88c2c6f69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release files / gzeus-0.1.2-cp39-abi3-win_amd64.whl

Download URL gzeus-0.1.2-cp39-abi3-win_amd64.whl
Size 184.4 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c3a3e2b61e8ef6a02ce0508e9b5d76cb70048437f3955c503e6de9ec6466abb5
BLAKE2b-256 checksum
How to use checksums
352ebe1269d186b0f5f21546b4c17161819c12b1fe37115627b9b8491b03c7a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release files / gzeus-0.1.2-cp39-abi3-manylinux_2_24_aarch64.whl

Download URL gzeus-0.1.2-cp39-abi3-manylinux_2_24_aarch64.whl
Size 261.7 kB
Tags CPython 3.9 Linux glibc 2.24+ ARM64 abi3
SHA-256 checksum
How to use checksums
8010d5e86f8008681ea48484b8e95920f93a38f3d5207cecdeee435ce1962bdf
BLAKE2b-256 checksum
How to use checksums
027d065843de61902a405c821bd509d45ca9c553e25a73e70ee8260221e52d2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release files / gzeus-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL gzeus-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 263.3 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
bedbecdcce93d018da883641cb766400ec659e008c2ce9ba882b2af69e879b95
BLAKE2b-256 checksum
How to use checksums
f9bbfdd01e5f3e9696af72a77f4aab04a4f9353750a8dec98d3323796318c2e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release files / gzeus-0.1.2-cp39-abi3-macosx_11_0_arm64.whl

Download URL gzeus-0.1.2-cp39-abi3-macosx_11_0_arm64.whl
Size 234.5 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f743d49375f8e7e4ee2185943d50cd4e44eb4504db2bb61e296b5947aa364b9e
BLAKE2b-256 checksum
How to use checksums
6fb6efe64ccf6b6a3d8a9e802874112837da501a1c1c753f3d0b56d6a0fa9c85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release files / gzeus-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl

Download URL gzeus-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl
Size 249.8 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
2123b826599133c856db4dd95d1788dc780922bfd8e01a953e20100d2c1c38fd
BLAKE2b-256 checksum
How to use checksums
5349ef5e12eb8a5ff1072bf6a3c469b41e0a96e1b77212535d64e87bfa0abbb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.9.6

Release history Release notifications | RSS feed

This release

0.1.2 This release

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page