Skip to content

py-utilz

Build Status Coverage Status Python Versions Platforms

Convenient helper functions, decorators, and data analysis tools to make life easier with minimal dependencies:

pip install py-utilz

dplyr like data grammar:

from utilz import pipe
import utilz.dfverbs as _

out = pipe(
    df,
    _.rename({"weight (male, lbs)": "male", "weight (female, lbs)": "female"}),
    _.pivot_longer(columns=["male", "female"], into=("sex", "weight")),
    _.split("weight", ("min", "max"), sep="-"),
    _.pivot_longer(columns=["min", "max"], into=("stat", "weight")),
    _.astype({"weight": float}),
    _.groupby("genus", "sex"),
    _.mutate(weight="weight.mean()"),
    _.pivot_wider(column="sex", using="weight"),
    _.mutate(dimorphism="male / female")
)

More convenient (parallel) looping:

from utilz import map

# Combine function results into a list, array, or dataframe
map(myfunc, myiterable)

# Syntactic sugar for joblib.Parallel
map(myfunc, myiterable, n_jobs=4)

Useful decorators for data analysis:

from utilz import log, maybe

# Print the shape of args and outputs before and after execute
@log
def myfunc(args):
    return out

# Only run myfunc if results.csv doesn't eist
@maybe
def myfunc(args, out_file=None):
    return out

myfunc(args, out_file='results.csv')

Checkout the overview page for more!