pandas_paddles.axis#
Module summary#
Select axis labels (columns or index) of a data frame.
Functions
|
|
|
Classes
|
API definition of the closure object. |
|
Combine two selection operators with a binary operator. |
|
Compose callable to select or sort columns. |
|
Compose callable to select columns by dtype. |
|
Select columns by dtype. |
Select all labels (i.e. |
|
Compose callable to select or sort index labels. |
|
Compose callable to select columns by name. |
|
Select labels by a predicate, e.g. |
|
Explicitely select labels. |
|
Compose callable to access multi-level index labels. |
|
Base-class for composing column/row selection operations. |
|
Container for selection along a data frame axis with combination logic. |
|
Base class to compose callable to select or sort axis labels (index and columns). |
|
Apply unary operator on selection operator. |
Details#
Classes
- class pandas_paddles.axis.BaseOp[source]#
Bases:
object
API definition of the closure object.
- __init__()#
- class pandas_paddles.axis.BinaryOp(left, right, op)[source]#
Bases:
pandas_paddles.axis.BaseOp
Combine two selection operators with a binary operator.
Used to implement, e.g.:
sel_1 | sel_2
- Parameters
left (pandas_paddles.axis.BaseOp) –
right (pandas_paddles.axis.BaseOp) –
op (Callable[[Any, Any], Any]) –
- __init__(left, right, op)[source]#
- Parameters
left (pandas_paddles.axis.BaseOp) –
right (pandas_paddles.axis.BaseOp) –
op (Callable[[Any, Any], Any]) –
- class pandas_paddles.axis.ColumnSelectionComposer(op=None, sample_size=None)[source]#
Bases:
pandas_paddles.axis.SelectionComposerBase
Compose callable to select or sort columns.
This acts as global entrypoint.
Use the global instance like:
# Move columns x, z to left from pandas_paddles import C df.loc[:, C["x", "z"] | ...]
Other use-cases:
Select slices of columns, e.g., when handling Excel-like named columns (A, B, …):
df.loc[:, C["B":"E"] | C["P":"S"]]
Select by dtype:
df.loc[:, C.dtype == str] df.loc[:, C.dtype == int] df.loc[:, C.dtype.isin((str, int))]
Note that for “non-trivial” dtypes (i.e. those stored in
object
-typed columns, e.g.str
), a subsample of the dataframe is tested explicitely. The sample-size can be set withsample_size
.Select all columns starting with
"PRE"
:df.loc[:, C.startswith("PRE")] # or just move them to the left and keep the remaining columns in # the data frame df.loc[:, C.startswith("PRE") | ...]
Access the level of a multi-index with:
C.levels[0] C.levels["level-name"]
Selections can be combined with
&
(intersection) and|
or+
(union). In intersections, the right-most order takes precedence, while it’s the left-most for unions, e.g. the following will select all columns with first-level label “b” starting with the columns with second-level labels “Y” and “Z” followed by all other second-level labels with first-level “b”:C.levels[0]["b"] & (C.levels[1]["Y"] | ...)
Inversion (negation) of selections is possible with
~
, e.g. to select all but first-level label “b”:~C.levels[0]["b"]
This can also be applied to composed selections:
~(C.levels[0]["b"] | C.levels[1]["X", "Y"])
- get_other_op(other)#
Get/create a wrapped operation for composing operations.
- property sample_size#
Sample size for dtype determination of object-typed columns.
- class pandas_paddles.axis.DtypeComposer(axis, sample_size=10)[source]#
Bases:
object
Compose callable to select columns by dtype.
- class pandas_paddles.axis.DtypesOp(dtypes, sample_size=10)[source]#
Bases:
pandas_paddles.axis.BaseOp
Select columns by dtype.
- Parameters
dtypes (Sequence) –
sample_size (int) –
- class pandas_paddles.axis.EllipsisOp[source]#
Bases:
pandas_paddles.axis.BaseOp
Select all labels (i.e. columns or rows).
- __init__()#
- class pandas_paddles.axis.IndexSelectionComposer(op=None)[source]#
Bases:
pandas_paddles.axis.SelectionComposerBase
Compose callable to select or sort index labels.
Note
Use
ColumnSelectionComposer
(C
) if you want to select columns.Use the global instance like:
# Move rows x, z to the top from pandas_paddles import I df.loc[I["x", "z"] | ...]
Other use-cases:
Select slices of rows:
df.loc[I["B":"E"] | I["P":"S"]]
Select all rows with index starting with
"PRE"
:df.loc[I.startswith("PRE")] # or just move them to the top and keep the remaining columns in # the data frame df.loc[I.startswith("PRE") | ...]
Access the level of a multi-index with:
I.levels[0] I.levels["level-name"]
Selections can be combined with
&
(intersection) and|
or+
(union). In intersections, the right-most order takes precedence, while it’s the left-most for unions, e.g. the following will select all rows with first-level label “b” starting with the rows with second-level labels “Y” and “Z” followed by all other second-level labels with first-level “b”:I.levels[0]["b"] & (I.levels[1]["Y"] | ...)
Inversion (negation) of selections is possible with
~
, e.g. to select all but first-level label “b”:~I.levels[0]["b"]
This can also be applied to composed selections:
~(I.levels[0]["b"] | I.levels[1]["X", "Y"])
- get_other_op(other)#
Get/create a wrapped operation for composing operations.
- class pandas_paddles.axis.LabelComposer(axis, op=None, level=None)[source]#
Bases:
pandas_paddles.axis.OpComposerBase
Compose callable to select columns by name.
Columns can be selected by name or string predicates: -
startswith
-endswith
-contains
-match
which are passed through topd.Series.str
.- get_other_op(other)#
Get/create a wrapped operation for composing operations.
- class pandas_paddles.axis.LabelPredicateOp(meth, args, kwargs, level=None)[source]#
Bases:
pandas_paddles.axis.BaseOp
Select labels by a predicate, e.g.
startswith
.
- class pandas_paddles.axis.LabelSelectionOp(labels, level=None)[source]#
Bases:
pandas_paddles.axis.BaseOp
Explicitely select labels.
- class pandas_paddles.axis.LeveledComposer(axis)[source]#
Bases:
object
Compose callable to access multi-level index labels.
- class pandas_paddles.axis.OpComposerBase(axis, op)[source]#
Bases:
object
Base-class for composing column/row selection operations.
This class wraps around the actual operation and overloads the relevant operators (
+
,&
,|
, and~
) and defers the evaluation of the operators until called (by the context data-frame in.loc[]
).- Parameters
axis (Literal['columns', 'index']) –
- class pandas_paddles.axis.Selection(included=None, excluded=None, *, mask=None)[source]#
Bases:
object
Container for selection along a data frame axis with combination logic.
- Parameters
included (Optional[List[int]]) –
excluded (Optional[List[int]]) –
mask (Optional[Sequence[int]]) –
- __init__(included=None, excluded=None, *, mask=None)[source]#
If
mask
is passed,included
andexcluded
must beNone
!- Parameters
included (Optional[List[int]]) – List of indices included in the selection.
excluded (Optional[List[int]]) – List of indices excluded from the selection.
mask (Optional[Sequence[int]]) – Boolean array that will be converted to list of included indices: All indices with corresponding truthy/non-zero value will be included in the selection.
- class pandas_paddles.axis.SelectionComposerBase(axis, op=None)[source]#
Bases:
pandas_paddles.axis.LabelComposer
Base class to compose callable to select or sort axis labels (index and columns).
- get_other_op(other)#
Get/create a wrapped operation for composing operations.
- class pandas_paddles.axis.UnaryOp(wrapped, op)[source]#
Bases:
pandas_paddles.axis.BaseOp
Apply unary operator on selection operator.
Used to implement, e.g., negation:
~sel
- Parameters
wrapped (pandas_paddles.axis.BaseOp) –
op (Callable[[Any], Any]) –
- __init__(wrapped, op)[source]#
- Parameters
wrapped (pandas_paddles.axis.BaseOp) –
op (Callable[[Any], Any]) –