Skip to content

Instantly share code, notes, and snippets.

View jmquintana79's full-sized avatar
💭
I may be slow to respond.

Juan Quintana jmquintana79

💭
I may be slow to respond.
View GitHub Profile
# 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:
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:
## 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)
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]
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
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.
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.
# 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
import numpy as np
from scipy.stats import t
def grubbs_test(data, alpha=0.05):
"""
Realiza el Grubbs' Test para detectar un único valor atípico en la muestra de datos.
Parameters:
data (list or numpy array): Lista de datos numéricos.
alpha (float): Nivel de significancia para el test. Default es 0.05.
df = df.astype({col: 'float32' for col in df.select_dtypes(include='float64').columns})