Package: pdpipewrench
PdPipeWrenchYAML-configurable Pandas pipelines.Thepdpipewrenchpackage reads input data, generates pipeline stages, and writes
output data entirely from the information supplied in a YAML configuration file. In
addition, custom-made or module-specific functions may be wrapped into pipeline stages
as specified in the YAML. Keyword arguments to such functions are also specified in
YAML, which sidesteps the problem of hard coding parameters into numerous*.pyfiles
for different datasets, each slightly different than the last.Installationpip install pdpipewrenchRequirementsThis package manages YAML configurations withconfuse, which itself depends onpyYAML. Pipeline stages and pipelines are generated withpdpipe, andengardeis an
optional dependency forverify_all-,verify_any-, andengarde-type stages.DetailsAll aspects of a pipeline are defined inconfig.yaml. This file contains information
aboutsources, files from which the data is drawn,pipelinesand their stages, and
thesinks, files to which the transformed data is written. Custom-made functions may
be defined in a standard*.pyfile/module, which must take apandas.DataFrameas
input and return apandas.DataFrameas output. Pipeline stages are generated from
these custom functions by specifying them and their keyword arguments inconfig.yaml.The fileconfig.yamlcontrols all aspects of the pipeline, from data discovery, to
pipeline stages, to data output. If the environment variablePDPIPEWRENCHDIRis not
specified, then then it will be set to the current working directory. The fileconfig.yamlshould be put in thePDPIPEWRENCHDIR, and data to be processed should be
in that directory or its subdirectories.ExampleThe directory structure of this example is as follows:example/
    config.yaml
    custom_functions.py
    example.py
    raw
        products_storeA.csv
        products_storeB.csv
    output
        products_storeA_processed.csv
        products_storeB_processed.csvThe contents ofconfig.yamlis as follows (paths are relative to the location ofconfig.yaml, i.e. thePDPIPEWRENCHDIR):sources:
  example_source:
    file: raw/products*.csv
    kwargs:
      usecols:
        - items
        - prices
        - inventory
    index_col: items

sinks:
  example_sink:
    file: output/*_processed.csv

pipelines:
  example_pipeline:

  - type: transform
      function: add_to_col
      kwargs:
        col_name: prices
        val: 1.5
      staging:
        desc: Adds $1.5 to column 'prices'
        exmsg: Couldn't add to 'prices'.

    - type: pdpipe
      function: ColDrop
      kwargs:
        columns: inventory
      staging:
        exraise: false

    - type: verify_all
      check: high_enough
      kwargs:
        col_name: prices
        val: 19
      staging:
        desc: Checks whether all 'prices' are over $19.The modulecustom_functions.pycontains:custom_functions.py

    def add_to_col(df, col_name, val):
        df.loc[:, col_name] = df.loc[:, col_name] + val
        return df

    def high_enough(df, col_name, val):
        return df.loc[:, col_name] > valFinally, the contents of the fileexample.py:import custom_functions
import pdpipewrench as pdpw

src = pdpw.Source("example_source")  # generate the source from `config.yaml`
snk = pdpw.Sink("example_sink")  # generate the sink from `config.yaml`.

# generate the pipeline from `config.yaml`.
line = pdpw.Line("example_pipeline", custom_functions)

# connect the source and sink to the pipeline, print what the pipeline will do, then run
# the pipeline, writing the output to disk. capture the input/output dataframes if desired.
pipeline = line.connect(src, snk)
print(pipeline)
(dfs_in, dfs_out) = line.run()Runningexample.pygeneratessrc,snk, andlineobjects. Then, thesrcandsnkare connected to an internalpipeline, which is apdpipe.PdPipeLineobject.
When this pipeline is printed, the following output is displayed:A pdpipe pipeline:
[ 0]  Adds $1.5 to column 'prices'
[ 1]  Drop columns inventory
[ 2]  Checks whether all 'prices' are over $19.The function of this pipeline is apparent from the descriptions of each stage. Some
stages have custom descriptions specified in thedesckey ofconfig.yaml. Stages
of typepdpipehave their descriptions auto-generated from the keyword arguments.The commandline.run()pulls data fromsrc, passes it throughpipeline, and
drains it tosnk. The returnsdfs_inanddfs_outshow that came in fromsrcand what went tosnk. In addition toline.run(), the firstnstages of the
pipeline can be tested on filemfrom the source withline.test(m,n).Output from ExampleThis is.\raw\products_storeA.csvbefore it is drawn into the source:itemspricesinventorycolorfoo195redbar243greenbaz227blueThis is.\raw\products_storeA.csvafter it is drawn into the source with the argumentusecols = ["items", "prices", "inventory"]specified inconfig.yaml:itemspricesinventoryfoo195bar243baz227The output from the pipeline is sent to.\products_storeA_processed.csv. The arguments
specified byconfig.yamlhave been applied. Namely,priceshave been incremented by1.5, theinventorycolumn has been dropped, and then a check has been made that allpricesare over19.itemspricesfoo20.5bar25.5baz23.5If theverify_allstep had failed, an exception would be raised, and the items that
did not pass the check would be returned in the exception message. Say, for example,
that thevalargument was21instead of19:AssertionError: ('high_enough not true for all',
prices  items        
foo      20.5)