Tool for pandera.
Project description
Pandera-Tool
Pandera-Tool is a package for pandera.
- DataFrame type definition
- Validation
- Type coerce
Installation
pip install pandera-tool
Example
DataFrame type definition
from io import StringIO
from typing import Tuple
import pandas as pd
from pandera import Field, SchemaModel
from pandera.errors import SchemaError
from pandera.typing import Series
from pandera_tool import check_annotations, dtype, to_dataframe, to_schema
@to_dataframe
class DataFrameIn(SchemaModel):
Name: Series[str] = Field()
class Config:
strict = True
@to_dataframe
class DataFrameOut(to_schema(DataFrameIn)):
Age: Series[int] = Field()
class Config:
strict = True
Validation
@check_annotations
def func_ok(df: DataFrameIn) -> DataFrameOut:
return df.assign(Age=2)
@check_annotations
def func_err(df: DataFrameIn) -> DataFrameOut:
return df.assign(Age="")
@check_annotations
def func_err_output_tuple(df: DataFrameIn) -> Tuple[DataFrameOut]:
return (df.assign(Age=""),)
df_ok = pd.DataFrame({"Name": ["1"]})
df_ng = pd.DataFrame({"Name": [1]})
func_ok(df_ok) # OK
try:
func_ok(df_ng) # Input error
except SchemaError as e:
print(e)
try:
func_err(df_ok) # Output error
except SchemaError as e:
print(e)
try:
func_err_output_tuple(df_ok) # Output error
except SchemaError as e:
print(e)
Type coerce
df1 = pd.read_csv(StringIO("Name\n1"))
try:
func_ok(df1) # Input error
except SchemaError as e:
print(e)
df2 = pd.read_csv(StringIO("Name\n1"), dtype=dtype(DataFrameIn))
func_ok(df2) # OK
df3 = df_ng.astype(dtype(DataFrameIn))
func_ok(df3) # OK
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file pandera_tool-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pandera_tool-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.2 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 084fe0c65cc7558ef82c5fd9a70c1398ad9572711dee356c6e9a300f74e65313 |
|
MD5 | fb849898ab6d0e4f12d0da3b62685b5e |
|
BLAKE2b-256 | 0cc4eb2decea05cfb58dbe464481422ef484bb6db3a04cd67acc42331757ec0d |