No project description provided
Project description
framehints
Simple library (decorator & function) for validating Pandas Dataframes structure
Installation
pip install git+https://github.com/lutrarutra/framehints
Usage
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
import numpy as np
import pandas as pd
import framehints as fh
@dataclass
class CustomClass():
field1: int
field2: str
schema = {
"name": str,
"value": float,
"date": datetime | None,
"path": Path | str,
"data": np.ndarray | list | None,
"custom": CustomClass,
"column_that_doesnt_exists": int # This column is optional, so it won't raise an error if it's missing
}
# Usage with function decorator
@fh.pdvalidate({"df": schema}, optional_columns={"df": ["column_that_doesnt_exists"]})
def your_function(df: pd.DataFrame):
return df.shape
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'value': [1.0, 2.5, 2.2],
'date': [datetime.now(), datetime.now(), None],
"path": [Path("/tmp"), Path("/var"), "/home"],
"data": [np.zeros([1, 2]), None, [1,2,3]],
"custom": [CustomClass(1, "a"), CustomClass(2, "b"), CustomClass(2, "b")]
})
try:
your_function(df)
print("1. DataFrame is valid.")
except TypeError as e:
print(e)
# or usage with function
try:
df = fh.validate_df(df, schema, optional_columns=["column_that_doesnt_exists"])
print("2. DataFrame is valid.")
except TypeError as e:
print(e)
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
framehints-1.0.1.tar.gz
(3.4 kB
view details)
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 framehints-1.0.1.tar.gz.
File metadata
- Download URL: framehints-1.0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6eb2bf7ccea0c0838129c86153183ae05d579bb922b5969fd6248eaf4096112
|
|
| MD5 |
9773257f829773390ba28a38e465deeb
|
|
| BLAKE2b-256 |
cfa251cf0dea39430339ec6294135d258c98410e80b3c139c9178a5f606fd11a
|
File details
Details for the file framehints-1.0.1-py3-none-any.whl.
File metadata
- Download URL: framehints-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbe8fd9f8b4b4f51de649a5e8dde2006d0f1e05fdef91e8acaf07fff8e243b91
|
|
| MD5 |
e12f7f5373bb40da5df9db705f7241fd
|
|
| BLAKE2b-256 |
e94eed303f309d80dae31840d2cc444db579858a4d4a13fb198d9662949a5d54
|