Skip to main content

Reduce pandas' dataframe memory usage

Project description

Dataframe Memory Project

This tools aims to provide simple solution to save memory when using pandas' data frame. It is highly inspired by this kaggle post.

[!IMPORTANT] The very basic principle : for each column, this tool reduces int and float precision as much as possible so that

  1. Approximate method method='approx' : no duplicated values appear and the minimum and maximum can be re-encoded
  2. Exact method method='exact' : preserve absolute information by testing every value.

For object data type, the function is trying to create category.

Usage

from data_memory import reduce_memory
import numpy as np
import pandas as pd

df = pd.DataFrame(
    np.array(
        [[1, 2, "aaa"],
         [4, 5, "bbb"],
         [7, 8, "ccc"]] * 10000),
    columns=['a', 'b', 'c'])

reduce_memory(df, method="exact", verbose=True)

Yields the following decrease of memory

Memory usage input: 5.04 MB
Memory usage output: 0.09 MB
Decreased by: 98.28 % 
df.info()
 #   Column  Non-Null Count  Dtype   
---  ------  --------------  -----   
 0   a       30000 non-null  category
 1   b       30000 non-null  category
 2   c       30000 non-null  category

[!WARNING] In method='approx',

  1. This tool destroys information and should not be applied automatically to any dataframe but big ones
  2. It preserves relative but not absolute information

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

dataframe_memory-2023.3.1.tar.gz (8.5 kB view hashes)

Uploaded Source

Built Distribution

dataframe_memory-2023.3.1-py3-none-any.whl (12.5 kB view hashes)

Uploaded Python 3

Supported by

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