util
File containing utility functions and classes.
cast_coef_to_string(df, column_name=COEF_KEY, drop_ones=True, float_precision=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
pl.DataFrame The input DataFrame. |
required | |
column_name
|
str, optional The name of the column to be casted. |
COEF_KEY
|
|
drop_ones
|
bool, optional If True, 1s are replaced with an empty string for non-constant terms. |
True
|
|
float_precision
|
int, optional The number of decimal places to round the coefficients to. If None, no rounding is done (so Polars' default precision is used). |
None
|
Examples: >>> import polars as pl >>> df = pl.DataFrame({"x": [1.0, -2.0, 1.0, 4.0], VAR_KEY: [1, 2, 0, 4]}) >>> cast_coef_to_string(df, "x") shape: (4, 2) ┌─────┬───────────────┐ │ x ┆ __variable_id │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════╪═══════════════╡ │ + ┆ 1 │ │ -2 ┆ 2 │ │ +1 ┆ 0 │ │ +4 ┆ 4 │ └─────┴───────────────┘
Source code in pyoframe/util.py
concat_dimensions(df, prefix=None, keep_dims=True, ignore_columns=RESERVED_COL_KEYS, replace_spaces=True, to_col='concated_dim')
Returns a new DataFrame with the column 'concated_dim'. Reserved columns are ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
pl.DataFrame The input DataFrame. |
required | |
prefix
|
str, optional The prefix to be added to the concated dimension. |
None
|
|
keep_dims
|
bool, optional If True, the original dimensions are kept in the new DataFrame. |
True
|
Examples:
>>> import polars as pl
>>> df = pl.DataFrame(
... {
... "dim1": [1, 2, 3, 1, 2, 3],
... "dim2": ["Y", "Y", "Y", "N", "N", "N"],
... }
... )
>>> concat_dimensions(df)
shape: (6, 3)
┌──────┬──────┬──────────────┐
│ dim1 ┆ dim2 ┆ concated_dim │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════╪══════════════╡
│ 1 ┆ Y ┆ [1,Y] │
│ 2 ┆ Y ┆ [2,Y] │
│ 3 ┆ Y ┆ [3,Y] │
│ 1 ┆ N ┆ [1,N] │
│ 2 ┆ N ┆ [2,N] │
│ 3 ┆ N ┆ [3,N] │
└──────┴──────┴──────────────┘
>>> concat_dimensions(df, prefix="x")
shape: (6, 3)
┌──────┬──────┬──────────────┐
│ dim1 ┆ dim2 ┆ concated_dim │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════╪══════════════╡
│ 1 ┆ Y ┆ x[1,Y] │
│ 2 ┆ Y ┆ x[2,Y] │
│ 3 ┆ Y ┆ x[3,Y] │
│ 1 ┆ N ┆ x[1,N] │
│ 2 ┆ N ┆ x[2,N] │
│ 3 ┆ N ┆ x[3,N] │
└──────┴──────┴──────────────┘
>>> concat_dimensions(df, keep_dims=False)
shape: (6, 1)
┌──────────────┐
│ concated_dim │
│ --- │
│ str │
╞══════════════╡
│ [1,Y] │
│ [2,Y] │
│ [3,Y] │
│ [1,N] │
│ [2,N] │
│ [3,N] │
└──────────────┘
>>> # Properly handles cases with no dimensions and ignores reserved columns
>>> df = pl.DataFrame({VAR_KEY: [1, 2]})
>>> concat_dimensions(df, prefix="x")
shape: (2, 2)
┌───────────────┬──────────────┐
│ __variable_id ┆ concated_dim │
│ --- ┆ --- │
│ i64 ┆ str │
╞═══════════════╪══════════════╡
│ 1 ┆ x │
│ 2 ┆ x │
└───────────────┴──────────────┘
Source code in pyoframe/util.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
dataframe_to_tupled_list(df, num_max_elements=None)
Converts a dataframe into a list of tuples. Used to print a Set to the console. See examples for behaviour.
Examples:
>>> df = pl.DataFrame({"x": [1, 2, 3, 4, 5]})
>>> dataframe_to_tupled_list(df)
'[1, 2, 3, 4, 5]'
>>> dataframe_to_tupled_list(df, 3)
'[1, 2, 3, ...]'
>>> df = pl.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 3, 4, 5, 6]})
>>> dataframe_to_tupled_list(df, 3)
'[(1, 2), (2, 3), (3, 4), ...]'
Source code in pyoframe/util.py
get_obj_repr(obj, _props=(), **kwargs)
Helper function to generate repr strings for classes. See usage for examples.
Source code in pyoframe/util.py
parse_inputs_as_iterable(*inputs)
Converts a parameter *x: Any | Iteraable[Any] to a single Iterable[Any] object. This is helpful to support these two ways of passing arguments: - foo([1, 2, 3]) - foo(1, 2, 3)
Inspired from the polars library.
Source code in pyoframe/util.py
unwrap_single_values(func)
Decorator for functions that return DataFrames. Returned dataframes with a single value will instead return the value.