Basic Pipeline Stages
Basic pdpipe PdPipelineStages.
Attributes
Classes
ColDrop
Bases: ColumnsBasedPipelineStage
A pipeline stage that drops columns by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
single label, list-like or callable
|
The label, or an iterable of labels, of columns to drop. Alternatively,
this parameter can be assigned a callable returning an iterable of
labels from an input pandas.DataFrame (see |
required |
errors |
Optional[str]
|
If āignoreā, suppress error and existing labels are dropped. |
āignoreā
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[8,'a'],[5,'b']], [1,2], ['num', 'char'])
>>> pdp.ColDrop('num').apply(df)
char
1 a
2 b
Source code in pdpipe/basic_stages.py
ValDrop
Bases: ColumnsBasedPipelineStage
A pipeline stage that drops rows by value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
list-like
|
A list of the values to drop. |
required |
columns |
single label, list-like or callable, default None
|
The label, or an iterable of labels, of columns to check for the given
values. Alternatively, this parameter can be assigned a callable
returning an iterable of labels from an input pandas.DataFrame. See
|
None
|
exclude_columns |
label, iterable or callable, optional
|
The label, or an iterable of labels, of columns to exclude, given the
|
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,5],[18,11]], [1,2,3], ['a','b'])
>>> pdp.ValDrop([4], 'a').apply(df)
a b
1 1 4
3 18 11
>>> pdp.ValDrop([4]).apply(df)
a b
3 18 11
Source code in pdpipe/basic_stages.py
ValKeep
Bases: ColumnsBasedPipelineStage
A pipeline stage that keeps rows by value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
list-like
|
A list of the values to keep. |
required |
columns |
single label, list-like or callable, default None
|
The label, or an iterable of labels, of columns to check for the given
values. Alternatively, this parameter can be assigned a callable
returning an iterable of labels from an input pandas.DataFrame. See
|
None
|
exclude_columns |
single label, iterable or callable, optional
|
The label, or an iterable of labels, of columns to exclude, given the
|
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,5],[5,11]], [1,2,3], ['a','b'])
>>> pdp.ValKeep([4, 5], 'a').apply(df)
a b
2 4 5
3 5 11
>>> pdp.ValKeep([4, 5]).apply(df)
a b
2 4 5
Source code in pdpipe/basic_stages.py
ColRename
Bases: PdPipelineStage
A pipeline stage that renames a column or columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rename_mapper |
dict-like or callable
|
Maps old column names to new ones. |
required |
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[8,'a'],[5,'b']], [1,2], ['num', 'char'])
>>> pdp.ColRename({'num': 'len', 'char': 'initial'}).apply(df)
len initial
1 8 a
2 5 b
>>> def renamer(lbl: str):
... if lbl.startswith('n'):
... return 'foo'
... return lbl
>>> pdp.ColRename(renamer).apply(df)
foo char
1 8 a
2 5 b
Source code in pdpipe/basic_stages.py
DropNa
Bases: PdPipelineStage
A pipeline stage that drops null values.
Supports all parameter supported by pandas.dropna function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported, as are all parameters of the pandas.dropna function. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,None],[1,11]], [1,2,3], ['a','b'])
>>> pdp.DropNa().apply(df)
a b
1 1 4.0
3 1 11.0
Source code in pdpipe/basic_stages.py
SetIndex
Bases: PdPipelineStage
A pipeline stage that set existing columns as index.
Supports all parameter supported by pandas.set_index function except for
inplace
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported, as are all parameters of the pandas.set_index function, except for 'inplace'. |
{}
|
Examples:
import pandas as pd; import pdpipe as pdp; df = pd.DataFrame([[1,4],[3, 11]], [1,2], ['a','b']) pdp.SetIndex('a').apply(df) b a 1 4 3 11
Source code in pdpipe/basic_stages.py
FreqDrop
Bases: PdPipelineStage
A pipeline stage that drops rows by value frequency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
int
|
The minimum frequency required for a value to be kept. |
required |
column |
str
|
The name of the colum to check for the given value frequency. |
required |
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,5],[1,11]], [1,2,3], ['a','b'])
>>> pdp.FreqDrop(2, 'a').apply(df)
a b
1 1 4
3 1 11
Source code in pdpipe/basic_stages.py
ColReorder
Bases: PdPipelineStage
A pipeline stage that reorders columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
positions |
dict
|
A mapping of column names to their desired positions after reordering. Columns not included in the mapping will maintain their relative positions over the non-mapped colums. |
required |
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[8,4,3,7]], columns=['a', 'b', 'c', 'd'])
>>> pdp.ColReorder({'b': 0, 'c': 3}).apply(df)
b a d c
0 4 8 7 3
Source code in pdpipe/basic_stages.py
RowDrop
Bases: ColumnsBasedPipelineStage
A pipeline stage that drops rows by callable conditions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conditions |
list-like or dict
|
The list of conditions that make a row eligible to be dropped. Each condition must be a callable that take a cell value and return a bool value. If a list of callables is given, the conditions are checked for each column value of each row. If a dict mapping column labels to callables is given, then each condition is only checked for the column values of the designated column. |
required |
reduce |
'any', 'all' or 'xor', default 'any'
|
Determines how row conditions are reduced. If set to 'all', a row must satisfy all given conditions to be dropped. If set to 'any', rows satisfying at least one of the conditions are dropped. If set to 'xor', rows satisfying exactly one of the conditions will be dropped. Set to 'any' by default. |
None
|
columns |
single label, iterable or callable, optional
|
The label, or an iterable of labels, of columns. Alternatively,
this parameter can be assigned a callable returning an iterable of
labels from an input pandas.DataFrame. See |
None
|
exclude_columns |
single label, iterable or callable, optional
|
The label, or an iterable of labels, of columns to exclude, given the
|
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,5],[5,11]], [1,2,3], ['a','b'])
>>> pdp.RowDrop([lambda x: x < 2]).apply(df)
a b
2 4 5
3 5 11
>>> pdp.RowDrop({'a': lambda x: x == 4}).apply(df)
a b
1 1 4
3 5 11
Source code in pdpipe/basic_stages.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
Schematize
Bases: PdPipelineStage
Enforces a column schema on input dataframes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
Optional[List[object]]
|
The dataframe schema to enforce on input dataframes. If set to None, the schema is learned in fit time and applied in subsequent transforms. |
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[2, 4, 8],[3, 6, 9]], [1, 2], ['a', 'b', 'c'])
>>> pdp.Schematize(['a', 'c']).apply(df)
a c
1 2 8
2 3 9
>>> pdp.Schematize(['c', 'b']).apply(df)
c b
1 8 4
2 9 6
Source code in pdpipe/basic_stages.py
DropDuplicates
Bases: ColumnsBasedPipelineStage
Drop duplicates in the given columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
ColumnsParamType
|
The labels of the columns to consider for duplication drop. If not populated, duplicates are dropped from all columns. |
None
|
exclude_columns |
object, iterable or callable, optional
|
The label, or an iterable of labels, of columns to exclude, given the
|
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[8, 1],[8, 2], [9, 2]], [1,2,3], ['a', 'b'])
>>> pdp.DropDuplicates('a').apply(df)
a b
1 8 1
3 9 2
Source code in pdpipe/basic_stages.py
ColumnDtypeEnforcer
Bases: PdPipelineStage
A pipeline stage enforcing column dtypes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_to_dtype |
Dict
|
Use {col: dtype, ā¦}, where col is a column label and dtype is a
numpy.dtype or Python type to cast one or more of the DataFrameās
columns to column-specific types. Alternatively, you can provide
|
required |
errors |
Optional[str]
|
Control raising of exceptions on invalid data for provided dtype. - raise : allow exceptions to be raised - ignore : suppress exceptions. On error return original object. |
'raise'
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[8,'a'],[5,'b']], [1,2], ['num', 'initial'])
>>> pdp.ColumnDtypeEnforcer({'num': float}).apply(df)
num initial
1 8.0 a
2 5.0 b
Source code in pdpipe/basic_stages.py
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 |
|
ConditionValidator
Bases: PdPipelineStage
A pipeline stage that validates boolean conditions on dataframes.
The stage does not change the input dataframe in any way.
The constructor expects either a single callable or a list-like of callable objects, and checks that all these callable return True - meaning all defined conditions hold - for input dataframes.
Naturally, pdpipe Condition
objects from the pdpipe.cond
module can be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conditions |
callable or list-like of callable
|
The conditions to check for input dataframes. Naturally, pdpipe
|
required |
reducer |
callable, optional
|
The callable that reduces the list of boolean result to a single
result. By default the built-in |
all
|
errors |
str, default 'raise'
|
If set to 'raise', the default, then if the result boolean result is
False a FailedConditionError is raised on stage application. If set to
'ignore', then conditions are checked, the results are printed if the
application was called with |
'raise'
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame([[1,4],[4,None],[1,11]], [1,2,3], ['a','b'])
>>> pdp.ConditionValidator(lambda df: len(df.columns) == 5).apply(df)
Traceback (most recent call last):
...
pdpipe.exceptions.FailedConditionError: ConditionValidator stage failed; some conditions did not hold for the input dataframe!
>>> pdp.ConditionValidator(pdp.cond.HasNoMissingValues()).apply(df)
Traceback (most recent call last):
...
pdpipe.exceptions.FailedConditionError: ConditionValidator stage failed; some conditions did not hold for the input dataframe!
Source code in pdpipe/basic_stages.py
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 |
|
ApplicationContextEnricher
Bases: PdPipelineStage
A pipeline stage that enriches the pipeline's application context.
Keyword arguments can be either PdPipelineStage constructor arguments, in which case they are passed to the stage constructor, or they can be mappings to be added to the application context. If a key maps to a callable, then the callable is called with the input dataframe as the first argument, and the application context is passed as a keyword argument if the callable expects it (otherwise only the input dataframe is passed), and the result is stored in the application context mapped by the key. If the key maps to a non-callable object, the mapping is simply stored in the application context.
Mappings are evaluated in the order they are passed to the constructor.
For example, ApplicationContextEnricher(suma=lambda df: df['a'].sum())
will add the sum of the 'a' column to the application context keys under
the key 'suma'. Lated stages can then access the value of 'suma' with
self.application_context['suma']
.
Similarly, ApplicationContextEnricher(b=5)
will add the {'b': 5} mapping
to the application context.
Gradual mappings that use earlier mappings should be given in order, e.g.:
ApplicationContextEnricher(
asum=lambda df: df['a'].sum(),
bsum=lambda df: df['a'].mean(),
absumdiff=lambda df, application_context: application_context['asum'] - application_context['bsum'],
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
str to object mapping
|
The mappings to be added to the application context. Also supports all PdPipelineStage constructor parameters. |
{}
|
Source code in pdpipe/basic_stages.py
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 |
|