Skip to content

Instantly share code, notes, and snippets.

@banditkings
banditkings / example_template.py
Last active February 1, 2022 21:25
Plotly Template Example
import plotly.graph_objects as go
import plotly.io as pio
pio.templates["intel"] = go.layout.Template(
layout_font = {'family': 'Intel Clear Light', 'size':12 },
layout_titlefont = {'family': 'Intel Clear', 'size':20},
layout = {'hovermode': 'x unified',
'colorway': ['#044c84', '#369bd6', '#b8b9bc', '#e6e7e9', '#f18b53',
'#007ec5', '#19d3f3', '#4c5050', '#fbc39b']}
@banditkings
banditkings / CustomCalendar.py
Last active March 21, 2023 23:45
helper function to create 5253WW calendar lookup
"""
Make a dataframe for a 52/53-workweek accounting period calendar given the last day of the previous year.
Since many companies use this 52/53 workweek for their fiscal periods, this is a useful function to quickly
build custom lookup tables so you can join between datasets!
"""
import pandas as pd
from datetime import datetime
import numpy as np

Poetry Checklist

A checklist of steps applied to install Poetry for the first time

Make sure you're on 1.4 or higher and follow the instructions that isolate poetry from the rest of your system (i.e. don't use brew if you're on Mac), so you don't have it tied to a specific python distro.

OSX/Linux Example:

@banditkings
banditkings / local_import.py
Created June 13, 2022 20:56
Import local package in an adjacent directory
"""
Code Snippet to do a local package import from a sibling directory in a local DS project
project_directory
├── notebooks
│ └── this_notebook.ipynb
└── src
└── my_package
├── __init__.py
└── my_module.py
@banditkings
banditkings / Julia cyberpunk theme.jl
Last active October 14, 2022 17:05
Julia Cyberpunk Theme for Makie
using CairoMakie
cyberpunk_theme = Theme(
# teal/cyan, pink, yellow, matrix green, red, violet
palette = (color =["#08F7FE", "#FE53BB", "#F5D300", "#00ff41", :red, "#9467bd"],),
backgroundcolor = "#212946",
textcolor=:gray90,
resolution=(640,320),
fontsize=12,
font = "Arial",
@banditkings
banditkings / julia_tidytuesday.jl
Created October 14, 2022 16:54
A basic example to pull in data from a tidytuesday repo, demonstrate DataFramesMeta syntax, and make a plot
using CSV, HTTP, DataFramesMeta, Plots, Dates
theme(:ggplot2)
plotlyjs()
file1 = "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-05-10/nyt_titles.tsv"
df = CSV.read(HTTP.get(file1).body, DataFrame)
# who is the author with the most weeks on the best seller list?
top_authors = @chain df begin
@banditkings
banditkings / palette.jl
Created December 22, 2022 20:53
Custom Color Palette from an Animated GIF in Julia
"""
Example:
```julia
# Include this file
include("palette.jl")
filepath = "data/image001.gif"
# Set a seed for reproducibility
@banditkings
banditkings / project_scaffold.md
Last active June 7, 2023 22:28
Starting a new DS project with cookiecutter, pyenv, poetry, pytest, git

Project Scaffolding

Let's start a brand new project here with:

  • cookiecutter: project templating
  • pyenv: managing virtual environments for different python versions
  • poetry: dependency management
  • pytest: testing
  • git: version control
@banditkings
banditkings / random_walk_anim.py
Last active October 6, 2023 23:12
Random Walk animation in Plotly
import numpy as np
import plotly.graph_objects as go
# A Random Walk animation in Plotly using a line and a marker for the current position
n = 200
# Random Walk with n points
x = np.arange(n)
i = 0
state = 0 # initial state
@banditkings
banditkings / timeseriesdata.py
Last active November 2, 2023 00:00
Python Time Series Datasets
# ------- STATSMODELS ---------
# Statsmodels has a few built in datasets as well as a utility for R-Datasets
# Yearly Nile River flows at Ashwan 1871-1970
import statsmodels.api as sm
df = sm.datasets.nile.load().data
df['ds'] = pd.date_range(start='1871', end='1970', freq='AS')
# Mauna Loa Weekly Atmospheric CO2 Data
import statsmodels.api as sm