Skip to main content

A Python library to reduce pandas DataFrame memory usage

Project description

🚀 SmolData

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

Python Version License: MIT PyPI version

🎯 Why SmolData?

Pandas DataFrames can consume massive amounts of memory, especially with large datasets. SmolData 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 smoldata 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 smoldata import MemoryOptimizer

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

2. Advanced Optimization (Recommended)

from smoldata 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 smoldata 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

SmolData 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

SmolData 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.0.tar.gz (118.5 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.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pandas_slim-0.1.0.tar.gz
  • Upload date:
  • Size: 118.5 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.0.tar.gz
Algorithm Hash digest
SHA256 617bb6d1c9077a88295436db5471a20324931eb29496536b3f65736680edbe68
MD5 fc45906cc625875b4835758be9e7fdf0
BLAKE2b-256 913f3b1e35c727b65fc767ce5d061b52974d5441016567132a4a41340250526d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pandas_slim-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a80f1f41d41c2bf69572ca0d7257d58e3e83c570ce6ebeb6392650578150f1b6
MD5 64e08737176bd603979c17a07f3d746d
BLAKE2b-256 775d592c8f0cdda7dee367ba8239de822b9b4064ab174c85b08c5277c51d9ba5

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