tidypolars_extra.helpers¶
Classes¶
Expressions that can be used in various contexts. |
Functions¶
|
Apply a function across a selection of columns |
|
Contains a literal string |
|
Mark a column to order in descending |
|
Ends with a suffix |
Selects all columns |
|
|
Get lagging values |
|
Matches pattern |
|
Starts with a prefix |
|
Select columns by type using a string |
Module Contents¶
- class tidypolars_extra.helpers.DescCol[source]¶
Bases:
polars.ExprExpressions that can be used in various contexts.
- tidypolars_extra.helpers.across(cols, fn=lambda x: ..., names_prefix=None, names_suffix=None)[source]¶
Apply a function across a selection of columns
- Parameters:
cols (list) – Columns to operate on
fn (lambda) – A function or lambda to apply to each column
names_prefix (Optional - str) – Prefix to append to changed columns
Examples
>>> df = tp.tibble(x = ['a', 'a', 'b'], y = range(3), z = range(3)) >>> df.mutate(across(['y', 'z'], lambda x: x * 2)) >>> df.mutate(across(tp.Int64, lambda x: x * 2, names_prefix = "double_")) >>> df.summarize(across(['y', 'z'], tp.mean), by = 'x')
- tidypolars_extra.helpers.contains(match, ignore_case=True)[source]¶
Contains a literal string
- Parameters:
match (str) – String to match columns
ignore_case (bool) – If TRUE, the default, ignores case when matching names.
Examples
>>> df = tp.tibble({'a': range(3), 'b': range(3), 'c': ['a', 'a', 'b']}) >>> df.select(contains('c'))
- tidypolars_extra.helpers.ends_with(match, ignore_case=True)[source]¶
Ends with a suffix
- Parameters:
match (str) – String to match columns
ignore_case (bool) – If TRUE, the default, ignores case when matching names.
Examples
>>> df = tp.tibble({'a': range(3), 'b_code': range(3), 'c_code': ['a', 'a', 'b']}) >>> df.select(ends_with('code'))
- tidypolars_extra.helpers.everything()[source]¶
Selects all columns
Examples
>>> df = tp.tibble({'a': range(3), 'b': range(3), 'c': ['a', 'a', 'b']}) >>> df.select(everything())
- tidypolars_extra.helpers.lag(x, n: int = 1, default=None)[source]¶
Get lagging values
- Parameters:
x (Expr, Series) – Column to operate on
n (int) – Number of positions to lag by
default (optional) – Value to fill in missing values
Examples
>>> df.mutate(lag_x = tp.lag(col('x'))) >>> df.mutate(lag_x = tp.lag('x'))
- tidypolars_extra.helpers.matches(match, ignore_case=False)[source]¶
Matches pattern
- Parameters:
match (str) – String to match columns
ignore_case (bool) – If True, the default, ignores case when matching names.
Examples
>>> df = tp.tibble({'a': range(3), 'add': range(3), 'sub': ['a', 'a', 'b']}) >>> df.select(tp.matches('a'))
- tidypolars_extra.helpers.starts_with(match, ignore_case=True)[source]¶
Starts with a prefix
- Parameters:
match (str) – String to match columns
ignore_case (bool) – If TRUE, the default, ignores case when matching names.
Examples
>>> df = tp.tibble({'a': range(3), 'add': range(3), 'sub': ['a', 'a', 'b']}) >>> df.select(starts_with('a'))
- tidypolars_extra.helpers.where(col_type)[source]¶
Select columns by type using a string
- Options:
character : factor (ordered or unordered) and string string : only strings, exclude factors factor : ordered or unordered factors ordered : only ordered factors unordered : only unordered factors
numeric : float or integet float : only float integer : only integer
date : date datetime : data and time
Examples
>>> from tidypolars_extra.data import mtcars >>> df = mtcars >>> df.select(tp.where("integer")) >>> df.select(tp.where("numeric")) >>> df.select(tp.where("string") | tp.where("integer"))