Skip to content

Instantly share code, notes, and snippets.

@DGrady
DGrady / plot_datetimes.py
Created August 18, 2017 19:27
Plot Pandas date times with matplotlib
"""
Most of the time, it’s pretty easy to take a Pandas data frame that contains
timestamps and plot it, for example using `df.plot(…)`. Nowadays many of
high-level Matplotlib functions are canny enough to handle Pandas timestamps on
their own as well.
But not all of them.
So how do you get nice handling of Pandas timestamps when you need to use
Matplotlib directly?
@DGrady
DGrady / subprocess_filter.py
Last active December 7, 2022 01:09
Stream data asynchronously through a subprocess in Python
"""
Problem: provide two-way communication with a subprocess in Python.
See also:
- https://kevinmccarthy.org/2016/07/25/streaming-subprocess-stdin-and-stdout-with-asyncio-in-python/
- http://eli.thegreenplace.net/2017/interacting-with-a-long-running-child-process-in-python/
"""
import asyncio
import sys
@DGrady
DGrady / sparkconf.py
Last active June 26, 2017 18:47
Configure PySpark environment variables and such
# See also https://github.com/minrk/findspark
import os
from pathlib import Path
import sys
def configure_spark(spark_home: str = None, python_path: str = None):
if not spark_home:
spark_home = os.environ['SPARK_HOME']
@DGrady
DGrady / describe_population.py
Last active August 18, 2018 04:16
Analyze data frames that contain mainly categorical (string) data
import pandas as pd
def describe_population(df: pd.DataFrame) -> pd.DataFrame:
"""
Report the populated and uniqueness counts for each column of the input.
The ratio columns are given as percents.
"""
N = len(df)
@DGrady
DGrady / extent.py
Last active November 12, 2017 19:50
Return the minimum and maximum values from an iterable in one pass
def extent(collection):
"""
Return the minimum and maximum values from an iterable in one pass
"""
itr = iter(collection)
first_value = next(itr)
minimum = first_value
maximum = first_value
@DGrady
DGrady / punctional.py
Last active October 19, 2016 23:28
Python, functional
# from operator import (
from cytoolz.curried.operator import (
add,
and_,
attrgetter,
# concat,
contains,
countOf,
delitem,
eq,
@DGrady
DGrady / abspath.zsh
Last active July 19, 2016 16:04
Shell (zsh) function for getting the absolute path of its argument
function abspath() {
# generate absolute path from relative path
# $1 : relative filename
# return : absolute path
# From http://stackoverflow.com/a/23002317/514210
if [[ -d "$1" ]]; then
# dir
(cd "$1"; pwd)
elif [[ -f "$1" ]]; then
# file
@DGrady
DGrady / custom.js
Created February 21, 2016 16:59
Automatically hide the toolbar and header in Jupyter Notebook 4.1.0
// Automatically hide the toolbar and header in Jupyter Notebook 4.1.0
// This should go in ~/.jupyter/custom/custom.js
require(
['base/js/namespace', 'base/js/events'],
function(Jupyter, events) {
events.on("notebook_loaded.Notebook", function () {
Jupyter.toolbar.actions.call('jupyter-notebook:toggle-toolbar')
Jupyter.toolbar.actions.call('jupyter-notebook:toggle-header')
})
}
@DGrady
DGrady / labeled-rule.tex
Last active August 29, 2015 14:20
Labeled rule in LaTeX
% \labeledrule[width]{Top}{Bottom}
%
% Useful for creating forms. The text above the rule is aligned to the
% surrounding baseline. A zero-width box contains the top label. The second
% command takes the bottom label and lowers it by \baselineskip, then packs it
% into a box with width zero, height of 1 em, and depth of \baselineskip plus
% the depth of the bottom label text. The height of the rule (0.4pt) is TeX's
% hard-coded height for \hrule.
\newcommand{\labeledrule}[3][\linewidth]{%