df-types
A Python tool for automatically generating dataclass definitions from pandas DataFrames.
Getting Started
pip install df-types
from df_types import DFTypes
import pandas as pd
import random
# Load your data
df = pd.DataFrame({
"id": list(range(1, 301)),
"name": ["Alice", "Bob", "Charlie"] * 100,
"age": [random.randint(18, 100) for _ in range(300)],
"prefers-pizza": [random.choice([True, False]) for _ in range(300)] # Not a valid Python identifier, will be normalized
})
# Generate type definitions
dft = DFTypes(df)
dft.write_types() # Creates typed_df.py
# Creates the following dataclass:
#
# @dataclass(slots=True)
# class TypedRowData:
# id: int
# name: Literal['Alice', 'Bob', 'Charlie']
# age: int
# prefers_pizza: bool
# Use the generated types
from typed_df import convert, iter_dataclasses
df_typed = convert(df) # Converts NaNs to None, normalizes column names to Python identifiers
for row_data in iter_dataclasses(df_typed):
# Each row_data is now a typed dataclass
print(f"ID: {row_data.id}, Name: {row_data.name}, Age: {row_data.age}, Prefers Pizza: {row_data.prefers_pizza}")
Features
Supported Types
| Feature | Example | Description |
|---|---|---|
| Literal Types | Literal["A", "B", "C"] |
For categorical data with known values |
| Union Types | int | float |
For columns with mixed numeric types |
| Optional Types | str | None |
For columns with missing values |
| Custom Types | pd.Timestamp, Decimal |
Import and use external types |
| Primitive Types | int, str, bool, float |
Standard Python types |
Configuration
from df_types.config import DFTypesConfig
# Basic options
config = DFTypesConfig(
filename="my_types.py",
class_name="MyRow", # Default is "TypedRowData"
max_literal_values=10 # Increase if you have more categories you want to infer as Literal types
)
dft = DFTypes(df, config=config)
dft.write_types()
from my_types import convert, iter_dataclasses, MyRow
# Use the generated types
Considerations
If a type cannot be imported from the generated file, it will be given the type hint object and a warning will be printed. Most often this occurs because the type is contained in the calling module (e.g., main.py which imports df_types and provides CustomType that is contained in the DataFrame). You can manually move the type definition to another file to avoid this warning.
Due to sampling, if you have a column with a large number of rows and a disproportionate distribution of values, the inferred literals may not include all possible values. You can increase sample_middle_rows, sample_head_rows, or sample_tail_rows if you want to sample more rows.
Future Features
- Support for typed containers (e.g.,
List[int],Dict[str, int]) - Support for nested dataclasses
- More advanced configuration options
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for df-types 0.0.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 | Provenance | Actions |
|---|---|---|---|---|
| df-types-0.0.2.tar.gz | 7.3 kB | Without provenance | Details for df-types-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| df_types-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for df_types-0.0.2-py3-none-any.whl |
Total release size: 15.8 kB
Release files / df-types-0.0.2.tar.gz
| Download URL | df-types-0.0.2.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e12a5ed0f2a99a995067b98ea6dfeecafe4369f6812e3c492619f6f3009077b1
|
|
BLAKE2b-256 checksum How to use checksums |
0bd0cb7490a7eb29539a3fce9574177e90ba41b706ceb04cc112a99853dfa300
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / df_types-0.0.2-py3-none-any.whl
| Download URL | df_types-0.0.2-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5f972f64d1422e0085be6146e086c395f40cc25efa953e2a6b9b7fa1bee9f3e7
|
|
BLAKE2b-256 checksum How to use checksums |
bc1f8f8106e210548f71d43302ff5c3bd8676c8c444179a803c9339b4d359ea4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for df-types 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.