Pandas Utils¶
sklego.pandas_utils.add_lags(X, cols, lags, drop_na=True)
¶
Appends lag column(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array - like
|
Data to be lagged. |
required |
cols
|
str | int | List[str] | List[int]
|
Column name(s) or index (indices). |
required |
lags
|
int | List[int]
|
The amount of lag for each col. |
required |
drop_na
|
bool
|
Whether or not to remove rows that contain NA values. |
True
|
Returns:
Type | Description |
---|---|
DataFrame | ndarray
|
With only the selected cols. |
Raises:
Type | Description |
---|---|
ValueError
|
If the input is not a supported DataFrame. |
Notes
Native cross-dataframe support is achieved using Narwhals. Supported dataframes are:
- pandas
- Polars (eager or lazy)
- Modin
- cuDF
See Narwhals docs for an up-to-date list (and to learn how you can add your dataframe library to it!).
Examples:
import pandas as pd
df = pd.DataFrame([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
columns=["a", "b", "c"],
index=[1, 2, 3]
)
add_lags(df, "a", [1]) # doctest: +NORMALIZE_WHITESPACE
'''
a b c a1
1 1 2 3 4.0
2 4 5 6 7.0
'''
add_lags(df, ["a", "b"], 2) # doctest: +NORMALIZE_WHITESPACE
'''
a b c a2 b2
1 1 2 3 7.0 8.0
'''
import numpy as np
X = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
add_lags(X, 0, [1])
# array([[1, 2, 3, 4],
# [4, 5, 6, 7]])
add_lags(X, 1, [-1, 1])
# array([[4, 5, 6, 2, 8]])
Source code in sklego/pandas_utils.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
sklego.pandas_utils.log_step(func=None, *, time_taken=True, shape=True, shape_delta=False, names=False, dtypes=False, print_fn=print, display_args=True, log_error=True)
¶
Decorates a function that transforms a pandas dataframe to add automated logging statements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable | None
|
The function to decorate with logs. If None, returns a partial function with the given arguments. |
None
|
time_taken
|
bool
|
Whether or not to log the time it took to run a function. |
True
|
shape
|
bool
|
Whether or not to log the shape of the output result. |
True
|
shape_delta
|
bool
|
Whether or not to log the difference in shape of input and output. |
False
|
names
|
bool
|
Whether or not to log the names of the columns of the result. |
False
|
dtypes
|
bool
|
Whether or not to log the dtypes of the result. |
False
|
print_fn
|
Callable
|
Print function to use (e.g. |
print
|
display_args
|
bool
|
Whether or not to display the arguments given to the function. |
True
|
log_error
|
bool
|
Whether or not to add the Exception message to the log if the function fails. |
True
|
Returns:
Type | Description |
---|---|
Callable
|
The decorated function. |
Examples:
@log_step
def remove_outliers(df, min_obs=5):
pass
@log_step(print_fn=logging.info, shape_delta=True)
def remove_outliers(df, min_obs=5):
pass
Source code in sklego/pandas_utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
|
sklego.pandas_utils.log_step_extra(*log_functions, print_fn=print, **log_func_kwargs)
¶
Decorates a function that transforms a pandas dataframe to add automated logging statements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*log_functions
|
List[Callable]
|
Functions that take the output of the decorated function and turn it into a log.
Note that the output of each log_function is casted to string using |
()
|
print_fn
|
Print function (e.g. |
print
|
|
**log_func_kwargs
|
Keyword arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Callable
|
The decorated function. |
Examples: