Package: dataenforce
Overviewdataenforceis a Python package used to enforce column names & types of pandas DataFrames using Python 3 type hinting.It is a common issue in Data Analysis to pass dataframes into functions without a clear idea of which columns are included or not, and as columns are added to or removed from input data, code can break in unexpected ways. Withdataenforce, you can provide a clear interface to your functions and ensure that the input dataframes will have the right format when your code is used.How to installInstall with pip:pip install dataenforceYou can also pip install it from the sources, or just import thedataenforcefolder.How to useThere are two parts indataenforce: the type-hinting part, and the validation. You can use type-hinting with the provided class to indicate what shape the input dataframes should have, and the validation decorator to additionally ensure the format is respected in every function call.Type-hinting:DatasetTheDatasettype indicates that we expect apandas.DataFrameColumn name checkingfrom dataenforce import Dataset

def process_data(data: Dataset["id", "name", "location"])
  passThe code above specifies thatdatamust be a DataFrame with exactly the 3 mentioned columns. If you want to only specify a subset of columns which is required, you can use an ellipsis:def process_data(data: Dataset["id", "name", "location", ...])
  passdtype checkingdef process_data(data: Dataset["id": int, "name": object, "latitude": float, "longitude": float])
  passThe code above specifies the column names which must be there, with associated types. A combination of only names & with types is possible:Dataset["id": int, "name"].Reusing dataframe formatsAs you're likely to use the same column subsets several times in your code, you can define them to reuse & combine them later:DName = Dataset["id", "name"]
DLocation = Dataset["id", "latitude", "longitude"]

# Expects columns id, name
def process1(data: DName):
  pass

# Expects columns id, name, latitude, longitude, timestamp
def process2(data: Dataset[DName, DLocation, "timestamp"])
  passEnforcing:@validateThe@validatedecorator ensures that inputDatasets have the right format when the function is called, otherwise raisesTypeError.from dataenforce import Dataset, validate
import pandas as pd

@validate
def process_data(data: Dataset["id", "name"]):
  pass

process_data(pd.DataFrame(dict(id=[1,2], name=["Alice", "Bob"]))) # Works
process_data(pd.DataFrame(dict(id=[1,2]))) # Raises a TypeError, column name missingHow to testdataenforceusespytestas a testing library. If you havepytestinstalled, just runPYTHONPATH="." pytestin the command line while being in the root folder.NotesYou can usedataenforceto type-hint the return value of a function, but it is not currently possible tovalidateit (it is not included in the checks)You can't use@validateon a function where you use non-base class type-hints as strings (likedef f() -> "MyClass"). Issue related to PEP 563This work is at experimental state. It is not production-ready. Please raise issues & send pull requests if you find/solve some bugsdataenforceis released under the Apache License 2.0, meaning you can freely use the library and redistribute it, provided Copyright is keptDependencies: Pandas & NumpyTested with Python 3.6, 3.7, 3.8