plydata.helper_verbs.create_if

class plydata.helper_verbs.create_if(*args, **kwargs)[source]

Create a new dataframe with columns selected by a predicate

Parameters
datadataframe, optional

Useful when not using the >> operator.

predicatefunction

A predicate function to be applied to the columns of the dataframe. Good candidates for predicate functions are those that check the type of the column. Such function are avaible at pandas.api.dtypes, for example pandas.api.types.is_numeric_dtype().

For convenience, you can reference the is_*_dtype functions with shorter strings:

'is_bool'             # pandas.api.types.is_bool_dtype
'is_categorical'      # pandas.api.types.is_categorical_dtype
'is_complex'          # pandas.api.types.is_complex_dtype
'is_datetime64_any'   # pandas.api.types.is_datetime64_any_dtype
'is_datetime64'       # pandas.api.types.is_datetime64_dtype
'is_datetime64_ns'    # pandas.api.types.is_datetime64_ns_dtype
'is_datetime64tz'     # pandas.api.types.is_datetime64tz_dtype
'is_float'            # pandas.api.types.is_float_dtype
'is_int64'            # pandas.api.types.is_int64_dtype
'is_integer'          # pandas.api.types.is_integer_dtype
'is_interval'         # pandas.api.types.is_interval_dtype
'is_numeric'          # pandas.api.types.is_numeric_dtype
'is_object'           # pandas.api.types.is_object_dtype
'is_period'           # pandas.api.types.is_period_dtype
'is_signed_integer'   # pandas.api.types.is_signed_integer_dtype
'is_string'           # pandas.api.types.is_string_dtype
'is_timedelta64'      # pandas.api.types.is_timedelta64_dtype
'is_timedelta64_ns'   # pandas.api.types.is_timedelta64_ns_dtype
'is_unsigned_integer' # pandas.api.types.is_unsigned_integer_dtype

No other string values are allowed.

functionscallable() or tuple or dict or str, optional

Functions to alter the columns:

  • function (any callable) - Function is applied to the column and the result columns replace the original columns.

  • tuple of functions - Each function is applied to all of the columns and the name (__name__) of the function is postfixed to resulting column names.

  • dict of the form {'name': function} - Allows you to apply one or more functions and also control the postfix to the name.

  • str - String can be used for more complex statements, but the resulting names will be terrible.

argstuple

Arguments to the functions. The arguments are pass to all functions.

kwargsdict

Keyword arguments to the functions. The keyword arguments are passed to all functions.

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from plydata import *
>>> df = pd.DataFrame({
...     'alpha': list('aaabbb'),
...     'beta': list('babruq'),
...     'theta': list('cdecde'),
...     'x': [1, 2, 3, 4, 5, 6],
...     'y': [6, 5, 4, 3, 2, 1],
...     'z': [7, 9, 11, 8, 10, 12]
... })

Create a new dataframe by doubling selected column values of the input frame. 'is_integer' is a shortcut to pdtypes.is_integer_dtype.

>>> def double(s):
...     return s + s
>>> df >> create_if('is_integer', double)
    x   y   z
0   2  12  14
1   4  10  18
2   6   8  22
3   8   6  16
4  10   4  20
5  12   2  24

Convert from centimetes to inches.

>>> def inch(col, decimals=0):
...     return np.round(col/2.54, decimals)
>>> def feet(col, decimals=0):
...     return np.round(col/30.48, decimals)
>>> df >> create_if('is_integer', (inch, feet), decimals=2)
   x_inch  y_inch  z_inch  x_feet  y_feet  z_feet
0    0.39    2.36    2.76    0.03    0.20    0.23
1    0.79    1.97    3.54    0.07    0.16    0.30
2    1.18    1.57    4.33    0.10    0.13    0.36
3    1.57    1.18    3.15    0.13    0.10    0.26
4    1.97    0.79    3.94    0.16    0.07    0.33
5    2.36    0.39    4.72    0.20    0.03    0.39

Group columns are always included, but they do not count towards the matched columns.

>>> (df
...  >> group_by('x')
...  >> create_if('is_integer', (inch, feet), decimals=2))
groups: ['x']
   x  y_inch  z_inch  y_feet  z_feet
0  1    2.36    2.76    0.20    0.23
1  2    1.97    3.54    0.16    0.30
2  3    1.57    4.33    0.13    0.36
3  4    1.18    3.15    0.10    0.26
4  5    0.79    3.94    0.07    0.33
5  6    0.39    4.72    0.03    0.39

Selecting columns that match a predicate.

>>> df >> create_if('is_integer')
   x  y   z
0  1  6   7
1  2  5   9
2  3  4  11
3  4  3   8
4  5  2  10
5  6  1  12