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.
🎯 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 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
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:
Results show memory reduction percentage and execution time for each optimization method.
🧠 How It Works
SmolData uses several intelligent techniques to reduce memory usage:
- Intelligent Downcasting: Automatically selects the smallest possible numeric dtype that can hold your data
- Smart Integer Detection: Identifies float columns that can be safely converted to integers
- Unsigned Integer Optimization: Uses unsigned integers when all values are positive
- Category Conversion: Converts repetitive string data to memory-efficient category dtype
- 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 optimizeint_cast(bool): Try to convert float columns to integers when possibleobj_to_category(bool): Convert object columns to category dtypesubset(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 strategybenchmark(df): Compare all optimization methodsplot_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.
- Fork the repository
- Create a feature branch
- Add tests for your changes
- 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
- Use
int_cast=Truefor datasets with float columns that might be integers - Enable
obj_to_category=Truefor datasets with repetitive string data - Process in chunks for extremely large datasets
- Benchmark first to choose the best strategy for your specific data
Made with 🔥 by Remi Ounadjela
Reduce your data, amplify your insights!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pandas_slim-0.1.1.tar.gz.
File metadata
- Download URL: pandas_slim-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4a8cf3f51edc3b9a2bbd98c185ce8322d1bc1eaca88c65ee9d613786e685800
|
|
| MD5 |
3581d2eaf7d28dacf84ff39ca4ae85e0
|
|
| BLAKE2b-256 |
d6a1ba074ef429bec2d057e4f2778bb05d115cbde9adc39445b1e48c3ec62c59
|
File details
Details for the file pandas_slim-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pandas_slim-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d0f345ae170c4402abf2e1a9e78b5867982b3418a239a6e110fc4bcb638adee
|
|
| MD5 |
edd8e501835a3f2d3e6475bbd41235f1
|
|
| BLAKE2b-256 |
69cb14167e2cfac0bbaabfe937cb17608824f5dcb9f42557200aa55c0a95c6ad
|