-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from data_transformers import transformer | ||
from pandas import DataFrame | ||
|
||
@transformer.convert | ||
def drop_col(df: DataFrame, col, axis=1): | ||
return df.drop(col, axis=axis) | ||
|
||
@transformer.convert | ||
def wide_to_long(df: DataFrame, primary_keys, value_name='valor', var_name='indicador'): | ||
return df.melt(id_vars=primary_keys, value_name=value_name, var_name=var_name) | ||
|
||
@transformer.convert | ||
def replace_value(df: DataFrame, col: str, curr_value: str, new_value: str): | ||
df = df.replace({col: curr_value}, new_value) | ||
return df | ||
|
||
@transformer.convert | ||
def sort_values(df: DataFrame, how: str, by: list): | ||
if how not in ['ascending', 'descending']: | ||
raise ValueError('how must be either "ascending" or "descending"') | ||
|
||
return df.sort_values(by=by, ascending=how=='ascending').reset_index(drop=True) | ||
|
||
@transformer.convert | ||
def cast_col(df: DataFrame, col: str, cast_to: type): | ||
df[col] = df[col].astype(cast_to) | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import io | ||
from pandas import DataFrame | ||
|
||
def get_dataframe_info(df): | ||
buf = io.StringIO() | ||
df.info(buf=buf) | ||
return buf.getvalue() | ||
|
||
def dict_to_str(d): | ||
return ','.join([f'{k}={v}' for k,v in d.items()]) | ||
|
||
def callstack_to_str(callstack): | ||
frames = [] | ||
for f, params, presult in callstack: | ||
frame = [] | ||
params.pop('df', None) | ||
params_str = dict_to_str(params) | ||
params_str = f'{f.name}({params_str})' | ||
frame.append(params_str) | ||
if isinstance(presult, DataFrame): | ||
info = '\n'.join(get_dataframe_info(presult).split('\n')[1:-3]) | ||
frame.append(info) | ||
frame.append('') | ||
frame.append(presult.head(1).to_markdown()) | ||
|
||
frames.append('\n'.join(frame)) | ||
frames.append('') | ||
frames.append('-'*30) | ||
frames.append('') | ||
|
||
return '\n'.join(frames) |