This file contains hidden or 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
| import gc | |
| ## --> here script content or loop-iteration | |
| # clean memory | |
| gc.collect() |
This file contains hidden or 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
| import os | |
| import pandas as pd | |
| import logging | |
| def list_files_recursive(folder_input:str, file_format:str = '') -> pd.DataFrame: | |
| """Collect a table of files in a given input folder with a selected format | |
| Arguments: | |
| folder_input {str} -- Input folder to be analized. | |
This file contains hidden or 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
| # pause and allow the selection between continue or stop | |
| while True: | |
| answer = input("Would you like to continue (C) or stop (S)? ").strip().lower() | |
| if answer == 'c': | |
| print("Continue...") | |
| break | |
| elif answer == 's': | |
| print("Stop.") | |
| exit() | |
| else: |
This file contains hidden or 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
| def ratio_lossed_gained_uncertainty(num_lossed:int, | |
| num_gained:int, | |
| alpha:float = 0.01, | |
| verbose:bool = False) -> tuple[float, float]: | |
| """Estimate ratio lossed / gained with statistical uncertainty | |
| Using confidence interval (CI) estimation for binomal estimation (loss, gain). | |
| NOTE> In this case lossed is win and gained is loss, that is, ratio = lossed / gained. | |
| Arguments: |
This file contains hidden or 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
| ## for example, append src folder from a module | |
| # when this command does not work | |
| sys.path.append("../") | |
| # replace by | |
| src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../')) | |
| sys.path.append(src_path) |
This file contains hidden or 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
| import pandas as pd | |
| import statsmodels.api as sm | |
| def analysis_lr(df:pd.DataFrame, l_columns_X:list, column_Y:str, alpha:float = .01)->pd.DataFrame: | |
| # validate arguments | |
| assert isinstance(df, pd.DataFrame) and len(df) > 0 | |
| for c in l_columns_X + [column_Y]: | |
| assert c in df.columns.tolist() | |
| # data preparation | |
| X = df[l_columns_X] |
This file contains hidden or 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
| import os | |
| import shutil | |
| def clean(path:str = '.')->None: | |
| """ | |
| Clean files and folders generated by Python: *.pyc y __pycache__. | |
| """ | |
| # initialize folders to be omited | |
| exclude_dirs = ["venv"] | |
| # loop of paths |
This file contains hidden or 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
| def create_agg_pivot_table_by_references(df:pd.DataFrame, | |
| col_values:str, | |
| col_var_ref_1:str, | |
| col_var_ref_2:str, | |
| sagg:str = 'median')->pd.DataFrame: | |
| """Create a pivot table by a given references and aggregator | |
| Arguments: | |
| df {pd.DataFrame} -- Data to be used. | |
| col_values {str} -- Name of column values to be used. |
This file contains hidden or 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
| import pandas as pd | |
| import numpy as np | |
| import copy | |
| import logging | |
| def counter_consecutive_frozen_values(df:pd.DataFrame, column:str)->pd.DataFrame: | |
| """Count consecutive frozen values | |
| It is added a new column 'counter' with counting values. | |
This file contains hidden or 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
| # validate dt column exits | |
| assert "dt_local" in df.columns.tolist() | |
| # make sure is a dt column | |
| df["dt_local"] = pd.to_datetime(df["dt_local"]) | |
| # set to local time (Europe/Madrid) | |
| s_local_time = "Europe/Madrid" | |
| df["local"] = df["dt_local"].dt.tz_localize(s_local_time, ambiguous=True) | |
| # conversion local to UTC | |
| df["dt_utc"] = df["local"].dt.tz_convert("UTC") | |
| # to dt |
NewerOlder