Skip to main content

PyPI version shields.io PyPI version shields.io PyPI version shields.io

Piper

Piper is a python package designed to simplify data wrangling tasks with pandas. It provides a set of wrapper functions or 'verbs' that provide a simpler interface to standard Pandas functions.

Piper functions accept and receive pandas dataframe objects. They can be used as standalone functions but are more powerful when used together in a Jupyter notebook cell to form a data pipeline. This is achieved by linking the functions using the '>>' link operator within a cell using %%piper magic command.

So, instead of the traditional pandas method of calling a method associated with an object, in this case showing the 'head' (first 5 rows) of the dataframe:

df.head() 

Piper passes the result of the dataframe object as the first parameter to the next function in the pipeline that, in turn, both accepts and returns dataframe objects. So the equivalent of above with piper is:

%%piper
df >> head() 

This 'chaining' or linking of functions provides a rapid, easy to use/remember approach to exploring, cleaning or building a data pipeline from csv, xml, excel, databases etc. Custom functions that accept and return dataframe objects can be linked together using this kind of syntax:

%%piper
read_oracle_database() 
>> validate_data()
>> cleanup_data()
>> generate_summary()
>> write_to_target_system()

The concept is based on the approach used in the R language tidyverse and magrittr packages. The main 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

Example #1 - A dataframe consisting of two columns A and B.

import pandas as pd
import numpy as np

np.random.seed(42)

df = pd.DataFrame({'A': np.random.randint(10, 1000, 10),
                   'B': np.random.randint(10, 1000, 10)})
df.head()
A B
0 112 476
1 445 224
2 870 340
3 280 468
4 116 97

Let's create two further calculated columns and filter the 'D' column values.

df['C'] = df['A'] + df['B']
df['D'] = df['C'] < 1000
df[df['D'] == False]
A B C D
2 870 340 1210 False
8 624 673 1297 False

The equivalent in piper would be:

%%piper
df 
>> assign(C = lambda x: x.A + x.B,
          D = lambda x: x.C < 1000)
>> where("~D")

Example #2 Suppose you need the following function to trim 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 by using the .pipe method:

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

The equivalent in piper would be to import the piper magic function, and the required '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('-dates') 
>> where(""" ~countries.isin(['Italy', 'Portugal']) &
              values_1 > 40 &
              values_2 < 25 """)
>> order_by('countries', ascending=False)
>> head(5)

--info option If you specify this option, you see the equivalent pandas 'piped' version below the cell.

%%piper --info
get_sample_data()
>> trim_columns()
>> select('-dates') 
>> where(""" ~countries.isin(['Italy', 'Portugal']) &
              values_1 > 40 &
              values_2 < 25 """)
>> order_by('countries', ascending=False)
>> head(5)

gives:

(get_sample_data()
.pipe(select, '-dates')
.pipe(where, """ ~countries.isin(['Italy', 'Portugal']) &values_1 > 40 &values_2 < 25 """)
.pipe(order_by, 'countries', ascending=False)
.pipe(head, 5))

Documentation

Further examples are available in these jupyter notebooks:

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

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

dpiper-0.0.9.tar.gz (61.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dpiper-0.0.9-py3-none-any.whl (70.8 kB view details)

Uploaded Python 3

File details

Details for the file dpiper-0.0.9.tar.gz.

File metadata

  • Download URL: dpiper-0.0.9.tar.gz
  • Upload date:
  • Size: 61.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for dpiper-0.0.9.tar.gz
Algorithm Hash digest
SHA256 842bfafa6d21f5787f4888d300c34d51022f422d4ba8433be82b9b705d11e7f8
MD5 83c06c3dcfb2b6d5a2eed170fcb3ac82
BLAKE2b-256 a8f53064562701099a1624941a804f92e1682a791e1dd45b0cbee1b649006e6a

See more details on using hashes here.

File details

Details for the file dpiper-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: dpiper-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 70.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for dpiper-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2f5f7c75f31ecbc07fa13520ff9d7343668a198b05793016c7d500b99111206b
MD5 704c4c3020d3531b88eec30b3f4ad836
BLAKE2b-256 8ab755219236769ebcddfc6581eb22a722d9a9b376ee22c24e77b177c689ce3b

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

This release

0.0.9 This release

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page