Skip to content

Instantly share code, notes, and snippets.

View otaviomguerra's full-sized avatar

Otávio Guerra otaviomguerra

  • CE, Brasil
View GitHub Profile
@otaviomguerra
otaviomguerra / normal_test.py
Created December 7, 2018 01:58
Teste k² de D'algostino para saber se uma variável tem distribuição normal ou nao
from scipy import stats
k2, p = stats.normaltest(Alturas.ALTURA.values)
alpha = 0.05
print("p = {:g}".format(p))
if p < alpha: # Hipótese nula: Os dados são de uma distribuição normal
print("A hipótese nula pode ser rejeitada")
else:
print("A hipótese nula não pode ser rejeitada")
@otaviomguerra
otaviomguerra / returns.py
Created December 26, 2018 00:28
Calculando retornos de um investimento, com preços em cada coluna do dataset com indice por data (serie temporal)
#dataset name = data
# Define your investment
investment = 1000
# Calculate the daily returns here
returns = data.pct_change()
# Calculate the cumulative returns here
returns_plus_one = returns + 1
cumulative_return = returns_plus_one.cumprod()
@otaviomguerra
otaviomguerra / ARMA.py
Created January 23, 2019 04:13
Fitting ARMA model for time series and choosing the best model with bayesian information criteria(lowest is the best)
# Import the module for estimating an ARMA model
from statsmodels.tsa.arima_model import ARMA
# Fit the data to an AR(p) for p = 0,...,6 , and save the BIC
BIC = np.zeros(7)
for p in range(7):
mod = ARMA(simulated_data_2, order=(p,0))
res = mod.fit()
# Save BIC for AR(p)
BIC[p] = res.bic
@otaviomguerra
otaviomguerra / cointegrated.py
Created January 23, 2019 20:00
Testing if 2 time series are cointegrated with ADF test
# Import the statsmodels module for regression and the adfuller function
import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
# Regress BTC on ETH
ETH = sm.add_constant(ETH)
result = sm.OLS(BTC,ETH).fit()
# Compute ADF
b = result.params[1]
@otaviomguerra
otaviomguerra / fix_dtypes.py
Last active March 23, 2019 22:17
Arrumando os tipos das variaveis no pandas. De "objeto" para "category" ou "float64"
variaveis_categoricas = [] #lista de variaveis categoricas
variaveis_continuas = [] #lista de variaveis continuas
for col in variaveis_categoricas:
dataset[col] = dataset[col].astype('category')
for col in variaveis_continuas:
dataset[col] = dataset[col].astype('float64') #pode ser int64
@otaviomguerra
otaviomguerra / multiple_encoding.py
Last active March 24, 2019 14:39
Encodando multiplas variaveis do dataset
dataset_encoded = dataset_raw.apply(LabelEncoder().fit_transform)
@otaviomguerra
otaviomguerra / multiple_read_csv.py
Last active February 14, 2020 19:07
Lendo varios arquivos no pandas de uma vez
import glob
import os
import pandas as pd
path = r'/path/to/Files'
all_files = glob.glob(os.path.join(path, "*.csv"))
df = pd.concat((pd.read_csv(f, sep = ';', header = None, encoding= 'ISO-8859-1') for f in all_files))
li = []
for filename in all_files:
@otaviomguerra
otaviomguerra / read_postgresql_query.py
Last active July 12, 2019 13:23
POSTGRESQL query para DataFrame
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://USER:PASSWORD@HOST/DATABASE')
query = "SELECT * FROM table"
df = pd.read_sql_query(query,engine)
@otaviomguerra
otaviomguerra / post.py
Last active July 4, 2019 12:56
POST request em api rest
campo1 = 'campo 1 do Json de request'
campo2 = 'campo 2 do Json de request'
# Dados de acesso da API
URL = 'https://link-da-api.app'
api_token = 'TOKEN DE ACESSO'
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer {0}'.format(api_token),
}
@otaviomguerra
otaviomguerra / df_to_table.py
Last active July 16, 2020 14:47
DataFrame para tabela no banco
from sqlalchemy import create_engine
import io
import pandas as pd
# Joga DataFrame diretamente no banco de dados
print("Carregando DataFrame no banco de dados...\n")
conn = engine.raw_connection()
cur = conn.cursor()
output = io.StringIO()