Function decorators for Pandas Dataframe column name and data type validation
Project description
DAFFY DataFrame Column Validator
Description
In projects using Pandas, it's very common to have functions that take Pandas DataFrames as input or produce them as output. It's hard to figure out quickly what these DataFrames contain. This library offers simple decorators to annotate your functions so that they document themselves and that documentation is kept up-to-date by validating the input and output on runtime.
Table of Contents
Installation
Install with your favorite Python dependency manager like
pip install daffy
or
poetry add daffy
Usage
Start by importing the needed decorators:
from daffy import df_in, df_out
To check a DataFrame input to a function, annotate the function with @df_in
. For example the following function expects to get
a DataFrame with columns Brand
and Price
:
@df_in(columns=["Brand", "Price"])
def process_cars(car_df):
# do stuff with cars
If your function takes multiple arguments, specify the field to be checked with it's name
:
@df_in(name="car_df", columns=["Brand", "Price"])
def process_cars(year, style, car_df):
# do stuff with cars
To check that a function returns a DataFrame with specific columns, use @df_out
decorator:
@df_out(columns=["Brand", "Price"])
def get_all_cars():
# get those cars
return all_cars_df
To check both input and output, just use both annotations on the same function:
@df_in(columns=["Brand", "Price"])
@df_out(columns=["Brand", "Price"])
def filter_cars(car_df):
# filter some cars
return filtered_cars_df
If you want to also check the data types of each column, you can replace the column array:
columns=["Brand", "Price"]
with a dict:
columns={"Brand": "object", "Price": "int64"}
This will not only check that the specified columns are found from the DataFrame but also that their dtype
is the expected.
Contributing
Contributions are accepted. Include tests in PR's.
Development
To run the tests, clone the repository, install dependencies with Poetry and run tests with PyTest:
poetry install
poetry shell
pytest
To enable linting on each commit, run pre-commit install
. After that, your every commit will be checked with isort
, black
and flake8
.
License
MIT
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
File details
Details for the file daffy-0.2.0.tar.gz
.
File metadata
- Download URL: daffy-0.2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/4.19.84-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eb702647984c2172540bf06ffc050122352c2d95e5dbdfb9cacf07345f6f931 |
|
MD5 | f5510ff76a024e1a6c79e100010a1fea |
|
BLAKE2b-256 | bd80acc0e8a9e567bbad491bc55c0fb6b885b546e52714b9be1bf75b18616802 |
File details
Details for the file daffy-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: daffy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/4.19.84-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6ecc7df57e412df3c20e6e7f0ec6d977d2686d16d494d2050f5509368b548c0 |
|
MD5 | 1b18cbb7acb3e77f56a24695b0f409d7 |
|
BLAKE2b-256 | 28a6ff4d8f14f01ed914bf9c5dbeda092e8a00cd20917aa360066af61f99f36e |