Skip to content

Instantly share code, notes, and snippets.

View christinebuckler's full-sized avatar

Christine christinebuckler

View GitHub Profile
@christinebuckler
christinebuckler / gist:079ec18eea9dc631f5e591dff9f041a2
Created May 9, 2024 17:10
Regex to get everything after the first space
\s(.*)
@christinebuckler
christinebuckler / ts_decompose.py
Created April 1, 2024 21:22
decompose the trend and seasonality components of a time-series forecast
# https://towardsdatascience.com/time-series-forecasting-based-on-the-trend-and-seasonal-components-26b92866e548
from statsmodels.tsa.seasonal import seasonal_decompose
def decompose(df):
decomposition = sm.tsa.seasonal_decompose(df, model='additive', freq=365)
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
fig = decomposition.plot()
@christinebuckler
christinebuckler / numpy_select.py
Last active November 27, 2023 19:05
how to use np select
np.select(condlist, choicelist, default=0)
# EXAMPLE
CondList = [
(data['FBG'] >= 126) | (data['HbA1c'] >= 6.5),
((data['FBG'] < 126) & (data['FBG'] >= 100)) | ((data['HbA1c'] < 6.5) & (data['HbA1c'] >= 5.7)),
(data['FBG'] < 100) | (data['HbA1c'] < 5.7)]
ChoiceList = [2,1,0]
data.insert(1,'DM',np.select(CondList,ChoiceList,default=3))
@christinebuckler
christinebuckler / convert_bytes.py
Created November 13, 2023 23:50
convert bytes to higher file size amounts
import math
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
@christinebuckler
christinebuckler / sql_loop_teradata.sql
Created October 30, 2023 16:18
Loop over sql in Teradata
-- In Teradata, you can use a Stored Procedure to loop over SQL statements with variable values.
-- Here is an example of how you might do this:
REPLACE PROCEDURE Test (OUT r2 VARCHAR (3000))
BEGIN
DECLARE RowCnt INT;
DECLARE i INT;
DECLARE CurrRow INT;
DECLARE r VARCHAR (3000);
@christinebuckler
christinebuckler / sql_loop.sql
Last active October 30, 2023 16:17
Loop over sql
declare @SomeTempTable table
(
Column1,
Column2
)
declare @FiscalWeek int = 1
declare @FinalWeek int = ?
while @FiscalWeek <= @FinalWeek
begin
insert into @SomeTempTable
# use standard HTML comments
<!-- This is commented out. -->
# Find
\((.*)
# Replace
($1) AS $1
# Example
,SUM(rtn_ttl_u
,SUM(rtn_ttl_u) AS rtn_ttl_u
# standard imports
import os
import logging
import time
import yaml
# third party imports
import pandas as pd
import dask.dataframe as dd
from dask.distributed import Client, LocalCluster
@christinebuckler
christinebuckler / os_stats.py
Created January 31, 2023 18:55
get number of cores and memory from OS
import os
import multiprocessing
# get the number of logical cpu cores
n_cores = os.cpu_count()
n_cores = multiprocessing.cpu_count()
# get total physical memory
# https://stackoverflow.com/questions/22102999/get-total-physical-memory-in-python
# For MacOS, as per user reports, this works with Python 3.7 but not with Python 3.8