Skip to main content

A python package to compress pandas DataFrames akin to Stata's `compress` command

Project description

df-compress

A python package to compress pandas DataFrames akin to Stata's compress command. This function may proove particularly helpfull if you are dealing with large datasets.

How to use

After installing the package use the following import:

from df_compress import compress

Example

It follows a reproducible example on df-compress usage:

from df_compress import compress
import pandas as pd
import numpy as np

df = pd.DataFrame(columns=["Year","State","Value"])
df.Year = np.random.randint(low=2000,high=2023,size=200).astype(str)
df.State = np.random.choice(['RJ','SP','ES','MT'],size=200)
df.Value= np.random.rand(200,1)

df = compress(df, show_conversions=True)

Which will print for you the transformations and memory saved:

Initial memory usage: 0.02 MB
Final memory usage: 0.00 MB
Memory reduced by: 0.02 MB (91.5%)

Variable type conversions:
column    from       to  memory saved (MB)
  Year  object    int16           0.009727
 State  object category           0.009178
 Value float64  float32           0.000763

Optional Parameters

The function has three optimal parameters (arguments):

  • convert_strings (bool): Whether to attempt to parse object columns as numbers
    • defaults to True
  • numeric_threshold (float): Indicates the proportion of valid numeric entries needed to convert a string to numeric
    • defaults to 0.999
  • show_conversions (bool): whether to report the changes made column by column
    • defaults to False

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

df_compress-0.4.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

df_compress-0.4.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file df_compress-0.4.0.tar.gz.

File metadata

  • Download URL: df_compress-0.4.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for df_compress-0.4.0.tar.gz
Algorithm Hash digest
SHA256 69ad843fd3344616f8f47609a06674937e73cb7104bae4498bcfd861377b8de3
MD5 5daffd35edcf8e9542e66f8e929a01f7
BLAKE2b-256 43ea35e648848f726ad680a58f1e864548e01a4fd6b38a3146cabd50f0eff124

See more details on using hashes here.

File details

Details for the file df_compress-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: df_compress-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for df_compress-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbad2d10c262d2ae61b5020acfcd506515f69b41f8c3252cc2f52dcc14291554
MD5 a2db7f22f82c3203a490e2798149eda6
BLAKE2b-256 b56564c853d232efc1296be5b13d390d1d2f21627bf8589f6e143125c7e91b2c

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