DataTypes Optimizer
A simple Python library to optimize the data types of a Pandas DataFrame, reducing memory usage.
How it works
The optimize_dtypes function in this library helps you reduce the memory footprint of your Pandas DataFrames. It works by downcasting numeric columns (both integers and floats) to their smallest possible data type that can still hold the data without any loss of precision.
For example, if you have a column of integers where the maximum value is 100, it's likely stored as an int64 by default in Pandas. This function will intelligently convert it to int8, which uses significantly less memory.
The library currently optimizes:
- Integer columns
- Float columns
It skips the following data types as they generally do not benefit from this type of downcasting:
- Object (string)
- Boolean
- Categorical
Usage
Here is a simple example of how to use the optimize_dtypes function:
import pandas as pd
import numpy as np
from datatypesoptimizer.dataOptimizer import optimize_dtypes
# Create a sample DataFrame
data = {
'integers': [1, 2, 100, 200],
'floats': [1.0, 2.5, 3.5, 4.5],
'strings': ['a', 'b', 'c', 'd']
}
df = pd.DataFrame(data)
print("Original DataFrame memory usage:")
print(df.memory_usage(deep=True))
print("\nOriginal dtypes:")
print(df.dtypes)
# Optimize the DataFrame
optimized_df = optimize_dtypes(df)
print("\nOptimized DataFrame memory usage:")
print(optimized_df.memory_usage(deep=True))
print("\nOptimized dtypes:")
print(optimized_df.dtypes)
Example Output
Original DataFrame memory usage:
Index 132
integers 32
floats 32
strings 244
dtype: int64
Original dtypes:
integers int64
floats float64
strings object
dtype: object
Optimized DataFrame memory usage:
Index 132
integers 4
floats 4
strings 244
dtype: int64
Optimized dtypes:
integers int8
floats float32
strings object
dtype: object
As you can see from the output, the memory usage for the integers and floats columns has been significantly reduced after optimization.
Installation
To use this library, you can clone the repository and import the optimize_dtypes function from the datatypesoptimizer.dataOptimizer module.
Metadata
Release files for datatypesoptimizer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| datatypesoptimizer-0.1.2.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datatypesoptimizer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / datatypesoptimizer-0.1.2.tar.gz
| Download URL | datatypesoptimizer-0.1.2.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07b1649fd4417857439efd59a2c292cf3d5209df50e55b5f3be793b052827861
|
|
BLAKE2b-256 checksum How to use checksums |
340dbf2f6aaf58d8070a6a00e553037c121663d02b063324649dda22a6734c46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.10
|
Release files / datatypesoptimizer-0.1.2-py3-none-any.whl
| Download URL | datatypesoptimizer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 3.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d27224ed710b804c38ab435134314964082d24773231e047e0096023ff544259
|
|
BLAKE2b-256 checksum How to use checksums |
ae7d6cc8ecf5e4505b2b5ff016e379c4cd50a5bfa6c52b698e08ab1e490afced
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.10
|