Skip to content

Instantly share code, notes, and snippets.

View jseabold's full-sized avatar

Skipper Seabold jseabold

View GitHub Profile
@jseabold
jseabold / webuse.py
Created November 28, 2011 04:25
Stata's webuse in python
import pandas
import numpy as np
def webuse(data, baseurl='http://www.stata-press.com/data/r11/'):
"""
Parameters
----------
data : str
Name of dataset to fetch.
@jseabold
jseabold / send_text.py
Created October 16, 2011 15:07
context manager to time code and send text message when done
"""
Context manager or function to send text messages to your phone when a
process is done.
Edit the global variables. You might be able to find your phone e-mail
address here: http://tinywords.com/about-old/mobile/
Usage:
with SendText("long running process"):
do_something()
@jseabold
jseabold / panelindex.py
Created September 8, 2011 21:21
pandas PanelIndex from MultiIndex
from pandas import MultiIndex, Factor
import numpy as np
import pandas
def _ensure_like_indices(time, panels):
n_time = len(time)
n_panel = len(panels)
u_panels = np.unique(panels) # this sorts!
u_time = np.unique(time)
if len(u_time) == n_time:
@jseabold
jseabold / mat2nparray.ado
Created August 21, 2011 18:35
Convert Stata matrices to numpy arrays
capture program drop mat2nparray
program define mat2nparray
version 11.0
syntax namelist(min=1), SAVing(str) [ Format(str) APPend REPlace ]
if "`format'"=="" local format "%16.0g"
local saving: subinstr local saving "." ".", count(local ext)
if !`ext' local saving "`saving'.py"
tempname myfile
file open `myfile' using "`saving'", write text `append' `replace'
file write `myfile' "import numpy as np" _n _n
@jseabold
jseabold / R2array.R
Created August 21, 2011 18:29
R matrix and vector to numpy array
# This function respects the digits option
mkarray <- function(X, name) {
cat(name); cat(" = np.array(["); cat(X, sep=","); cat("])")
if (is.matrix(X)) {
i <- as.character(nrow(X))
j <- as.character(ncol(X))
cat(".reshape("); cat(i); cat(","); cat(j); cat(", order='F')")
}
cat("\n\n")