Skip to content

Deterministic gzip compressed outputs #28103

Closed
@dhimmel

Description

@dhimmel

GZ-compression writes the filename and timestamp into compressed data's header. This means that compressing the same data at different times will produce outputs that are not byte-for-byte identical.

In the past, this has presented problems for OS distros. It now prtesents problems for data science. Specifically, I frequently track compressed files using Git LFS which detects whether a file has changed by its hash. Therefore, if I run a pipeline to create gzip-compressed dataframes exported from Pandas, the .gz outputs will differ every time.

Currently, user's can use this hack which globally sets gzip.time to a fake time to create deterministic gzip compression from pandas.DataFrame.to_csv. I propose either of the following approaches that would be much cleaner:

  1. changing Pandas' default behavior to set gzip's mtime to a constant but erroneous time. Whatever gzip --no-name sets would probably be best.

  2. Having some module or function level setting that users could activate for deterministic gzip compression.

Personally, I don't see much benefit to gzip's timestamp, and therefore prefer solution 1 to 2. It's pretty confusing to users to see gzip outputs change and have to figure out that it's the changing timestamp.

In either case, we should look into the other supported compression methods and check their determinism. We can check time-dependent output with:

import gzip, time
data = b'data to compress'
output_1 = gzip.compress(data)
time.sleep(2)
output_2 = gzip.compress(data)
output_1 == output_2

Here's the docs for gzip --no-name

-n --no-name
When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had to be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the compressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This option is the default when decompressing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions