Piper
Piper is a python module to simplify data wrangling with pandas using a set of wrapper functions. These functions or 'verbs' attempt to provide a simpler interface to standard Pandas functions.
When combined within a Jupyter notebook using the %%piper magic command, a simple data 'pipeline' that looks rather like SQL syntax can be built.
The concept is similar to the way R's tidyverse and magrittr libraries are used.
The main dataframe manipulation functions are:
- select()
- assign()
- relocate()
- where()
- group_by()
- summarise()
- order_by()
For other piper functionality, please see the Goals and Features section.
Alternatives
For a comprehensive alternative, please check out Michael Chow's siuba package.
Table of contents
Installation
To install the package, enter the following:
pip install dpiper
Basic use
Suppose you need the following function to trim a given dataframes columnar text data.
def trim_columns(df):
''' Trim blanks for given dataframe '''
str_cols = df.select_dtypes(include='object').columns
for col in str_cols:
df[col] = df[col].str.strip()
return df
Standard Pandas can combine the new function into a pipeline along with other transformation/filtering tasks:
import pandas as pd
from piper.factory import get_sample_data
df = get_sample_data()
# Select all columns EXCEPT 'dates'
subset_cols = ['order_dates', 'regions', 'countries', 'values_1', 'values_2']
criteria1 = ~df['countries'].isin(['Italy', 'Portugal'])
criteria2 = df['values_1'] > 40
criteria3 = df['values_2'] < 25
df2 = (df[subset_cols][criteria1 & criteria2 & criteria3]
.pipe(trim_columns)
.sort_values('countries', ascending=False))
df2.head()
Result:
| dates | order_dates | countries | ids | values_1 | values_2 |
|---|---|---|---|---|---|
| 2020-03-03 | 2020-03-09 | Sweden | E | 194 | 20 |
| 2020-05-02 | 2020-05-08 | Sweden | D | 322 | 14 |
| 2020-01-20 | 2020-01-26 | Spain | A | 183 | 20 |
| 2020-02-01 | 2020-02-07 | Norway | D | 344 | 21 |
| 2020-05-06 | 2020-05-12 | Norway | B | 135 | 21 |
Using piper
Piper tries to improve this pipeline approach. Let's import piper's %%piper magic command and piper 'verbs'.
from piper import piper
from piper.verbs import head, select, where, group_by, summarise, order_by
Using the %%piper magic function, piper verbs can be combined with standard python functions like trim_columns() using the linking symbol '>>' to form a data pipeline.
%%piper
get_sample_data()
>> trim_columns()
>> select('-regions')
>> where(""" ~countries.isin(['Italy', 'Portugal']) &
values_1 > 40 &
values_2 < 25 """)
>> order_by('countries', ascending=False)
>> head(5)
Goals and Features
- Enhance working with Excel files through the WorkBook class
- Exporting high quality formatted Excel Workbooks using xlsxwriter
- Provide access to databases with support for SQL based scripting and connections.
To-do list
- TBD
Documentation
Further examples are available in these jupyter notebooks:
Status
Project has just started. I welcome any and all help to improve etc.
Inspiration
Pandas and numpy are amazing data analysis libraries. My goal is to combine their power with the convenience and ease of use of the R tidyverse package suite.
Contact
This is very much a personal library, in that its highly opinionated, flawed and probably of no use to anyone else :). However if it helps anyone else in their endeavours, that would be fantastic to hear about.
If you'd like to contact me, I'm miketarpey@gmx.net.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dpiper-0.0.8.tar.gz.
File metadata
- Download URL: dpiper-0.0.8.tar.gz
- Upload date:
- Size: 55.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae9ed27cd9b97f572f2d91f2dfb13d950217b41409e5f8bfdb2176e072dcb29
|
|
| MD5 |
596b36e997cdd59a41bc39bd528035ad
|
|
| BLAKE2b-256 |
32995a6305290c9f3ec6c0057e7da6f06fc3b63489673e9c6687e0156c9d465f
|
File details
Details for the file dpiper-0.0.8-py3-none-any.whl.
File metadata
- Download URL: dpiper-0.0.8-py3-none-any.whl
- Upload date:
- Size: 64.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f890d91d17ba5f03a15da177cb712ff946a01d27ad0b58510fc79cd6a89a8e80
|
|
| MD5 |
d2e269dfde5449d2f49cb8827a6d0577
|
|
| BLAKE2b-256 |
bce091715872ca6752dffe18b3b5e934769733f1e2aabe130447982af1796cab
|