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 | |
def generate_random_dates(num_dates: int) -> np.array: | |
"""Generate a 1D array of `num_dates` random dates. | |
""" | |
start_date = "2020-01-01" | |
# Generate all days for 2020 | |
available_dates = [np.datetime64(start_date) + days for days in range(365)] | |
# Get `num_dates` random dates from 2020 |
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 matplotlib.pyplot as plt | |
import numpy as np | |
# Load the CSV (load date data as proper date types) | |
df = pd.read_csv("page_views.csv") | |
df["date"] = pd.to_datetime(df["date"]) | |
# Sort the DF from oldest to most recent recordings | |
df.sort_values(by="date", inplace=True) | |
# Use the column of dates as the DF's index |
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
# Pivot the DF so that there's a column for each month, each row\ | |
# represents a year, and the cells have the mean page views for the\ | |
# respective year and month | |
df_pivot = pd.pivot_table( | |
df, | |
values="page_views", | |
index="year", | |
columns="month", | |
aggfunc=np.mean | |
) |
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 matplotlib.pyplot as plt | |
import numpy as np | |
# Load the CSV (load date data as proper date types) | |
df = pd.read_csv("page_views.csv") | |
df["date"] = pd.to_datetime(df["date"]) | |
# Sort the DF from oldest to most recent recordings | |
df.sort_values(by="date", inplace=True) | |
# Use the column of dates as the DF's index |
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
0.70 - Coffee | |
0.75 - Cake slice | |
19.99 - Video game | |
12 - Lunch | |
6.99 - Spotify monthly charge | |
22.24 - Travel mug | |
7 - Cinema ticket | |
15 - Lunch | |
Total: 85.62 |
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 re | |
import datetime | |
import pandas as pd | |
PATTERN = r'(^([\d]+)([\.]?)([\d]*))( - )(.*)' | |
# Load text | |
with open("expenses.txt", "r") as f: | |
expenses_txt = f.readlines() | |
# Put all the lines into a single string |
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 | |
# Current employee dataset | |
original = pd.DataFrame({ | |
"id": [1, 2, 3, 4], | |
"name": ["Michael", "Jim", "Pam", "Dwight"], | |
"age": [46, 35, 35, 38], | |
"email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"], | |
"in_company": [True, True, True, True] |
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 get_text_dimensions(text_string, font): | |
# https://stackoverflow.com/a/46220683/9263761 | |
ascent, descent = font.getmetrics() | |
text_width = font.getmask(text_string).getbbox()[2] | |
text_height = font.getmask(text_string).getbbox()[3] + descent | |
return (text_width, text_height) |
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
from PIL import Image, ImageDraw, ImageFont | |
from textwrap import wrap | |
def get_y_and_heights(text_wrapped, dimensions, margin, font): | |
"""Get the first vertical coordinate at which to draw text and the height of each line of text""" | |
# https://stackoverflow.com/a/46220683/9263761 | |
ascent, descent = font.getmetrics() | |
# Calculate the height needed to draw each line of text (including its bottom margin) | |
line_heights = [ |
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
from configparser import ConfigParser | |
import psycopg2 | |
import psycopg2.extras as psql_extras | |
import pandas as pd | |
from typing import Dict, List | |
def load_connection_info( | |
ini_filename: str | |
) -> Dict[str, str]: |