Skip to main content

A Python library to reduce pandas DataFrame memory usage

Project description

🚀 pandas-slim

A high-performance Python library for dramatically reducing pandas DataFrame memory usage through intelligent optimization techniques.

Python Version License: MIT PyPI version

🎯 Why pandas-slim?

Pandas DataFrames can consume massive amounts of memory, especially with large datasets. pandas-slim provides scientifically-tested memory optimization techniques that can reduce your DataFrame memory usage by up to 87% while maintaining data integrity.

📦 Installation

pip install pandas-slim

⚡ Quick Start

import pandas as pd
from pandas_slim import reduce_mem_usage
from sklearn.datasets import load_wine

# Load your DataFrame
df = pd.DataFrame(load_wine().data, columns=load_wine().feature_names)
print(f"Original memory: {df.memory_usage().sum() / 1024**2:.2f} MB")

# Reduce memory usage with one line!
df_optimized = reduce_mem_usage(df)
# Output: Memory usage decreased by 75.4%!

🧪 Scientific Benchmark Results

We tested 7 different optimization methods on the sklearn wine dataset. Here are the results:

🏆 Performance Summary

Method Memory Reduction Speed Best For
🥇 Quantized 86.9% 0.0024s Maximum compression
🥈 Advanced (User v1) 75.4% 0.0446s Balanced optimization
🥉 Aggressive 75.4% 0.0023s Fast & effective
Basic 74.5% 0.0010s Quick optimization
Sparse Matrix 74.5% 0.0022s Sparse data
Precision Loss (16-bit) 74.5% 0.0009s Fastest method
Precision Loss (8-bit) 74.5% 0.0009s Ultra-fast

📊 Detailed Benchmark

Benchmark Results

Key Findings:

  • 🎯 Quantized method achieved the highest memory reduction at 86.9%
  • Precision Loss (16-bit) was the fastest with 0.0009s execution time
  • 🏅 Most efficient method: Precision Loss (16-bit) with efficiency score of 81,107.7
  • 📈 All methods successfully reduced memory usage by 74.5% or more

🔧 Features

Multiple Optimization Strategies

1. Basic Optimization

from pandas_slim import MemoryOptimizer

optimizer = MemoryOptimizer(strategy='basic')
df_basic = optimizer.optimize(df)

2. Advanced Optimization (Recommended)

from pandas_slim import reduce_mem_usage

# Smart integer detection and unsigned integer usage
df_advanced = reduce_mem_usage(df, int_cast=True, obj_to_category=False)

3. Aggressive Optimization

optimizer = MemoryOptimizer(strategy='aggressive')
df_aggressive = optimizer.optimize(df)

Scientific Benchmarking

from pandas_slim import MemoryOptimizer

optimizer = MemoryOptimizer()

# Get detailed benchmark results
results = optimizer.benchmark(df)
print(results)

# Create visualization
optimizer.plot_benchmark(df, save_path='benchmark.png')

📊 Benchmark Results

Here's how different optimization methods perform on the Wine dataset:

Benchmark Results

Results show memory reduction percentage and execution time for each optimization method.

🧠 How It Works

pandas-slim uses several intelligent techniques to reduce memory usage:

  1. Intelligent Downcasting: Automatically selects the smallest possible numeric dtype that can hold your data
  2. Smart Integer Detection: Identifies float columns that can be safely converted to integers
  3. Unsigned Integer Optimization: Uses unsigned integers when all values are positive
  4. Category Conversion: Converts repetitive string data to memory-efficient category dtype
  5. Float Precision Optimization: Reduces float precision when possible without data loss

Supported Data Types

Original Type Optimized Options
int64 int8, int16, int32, uint8, uint16, uint32
float64 float16, float32, or convert to integer
object category (optional)

📚 API Reference

reduce_mem_usage(df, int_cast=True, obj_to_category=False, subset=None, verbose=True)

Main function to reduce DataFrame memory usage.

Parameters:

  • df (pd.DataFrame): DataFrame to optimize
  • int_cast (bool): Try to convert float columns to integers when possible
  • obj_to_category (bool): Convert object columns to category dtype
  • subset (list): Specific columns to optimize (default: all columns)
  • verbose (bool): Print optimization progress and results

Returns:

  • pd.DataFrame: Optimized DataFrame

MemoryOptimizer(strategy='advanced')

Class-based interface for memory optimization.

Methods:

  • optimize(df, **kwargs): Optimize DataFrame using selected strategy
  • benchmark(df): Compare all optimization methods
  • plot_benchmark(df, save_path=None): Visualize benchmark results

🎯 Use Cases

  • Large Dataset Processing: Reduce memory usage for big data analysis
  • Machine Learning Pipelines: Optimize feature matrices before training
  • Data Warehousing: Compress data for storage and transfer
  • Memory-Constrained Environments: Maximize available RAM usage
  • Performance Optimization: Speed up operations on large DataFrames

🔬 Scientific Approach

pandas-slim is built with a scientific mindset:

  • Benchmarking: Built-in tools to measure and compare optimization methods
  • Reproducibility: Consistent results across different datasets
  • Validation: Extensive testing to ensure data integrity
  • Transparency: Clear reporting of memory savings and processing time

🤝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. Submit a pull request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by the excellent work at mikulskibartosz.name
  • Built with ❤️ for the data science community

📈 Performance Tips

  1. Use int_cast=True for datasets with float columns that might be integers
  2. Enable obj_to_category=True for datasets with repetitive string data
  3. Process in chunks for extremely large datasets
  4. Benchmark first to choose the best strategy for your specific data

Made with 🔥 by Remi Ounadjela

Reduce your data, amplify your insights!

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

pandas_slim-0.1.2.tar.gz (118.6 kB view details)

Uploaded Source

Built Distribution

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

pandas_slim-0.1.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file pandas_slim-0.1.2.tar.gz.

File metadata

  • Download URL: pandas_slim-0.1.2.tar.gz
  • Upload date:
  • Size: 118.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pandas_slim-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dd81206bd09ed9dfe5a78eb669ade46aa239b4f6792f5b9b6a5cc6f10af4307a
MD5 fec1f9d6db3fd76f34d065916647f153
BLAKE2b-256 29a8b3d9670cd04cb9997280ddd01660be3156839c05a2d842ff0532e1eec1e3

See more details on using hashes here.

File details

Details for the file pandas_slim-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pandas_slim-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pandas_slim-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 465c9778f2afc4f8f16e21bf9ed9e939b523988beed5f6c4e56d6d4b9c5e008d
MD5 d410d7e9dd84844e02b59bd1b166f2e2
BLAKE2b-256 29178b210fa7bf1e5dff8d4bd346946e860f7a257e276623c9bf72804090a262

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