Skip to content

Instantly share code, notes, and snippets.

import pandas as pd
df = pd.read_excel('2016_Bike_Share_Toronto_Ridership_Q4.xlsx')
for col in ['trip_start_time', 'trip_stop_time']:
df[col] = pd.to_datetime(df[col])
for col in ['trip_start_time', 'trip_stop_time']:
swapped = pd.to_datetime({'year':df[col].dt.year,
'month':df[col].dt.day,
'day':df[col].dt.month,
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
img = np.random.randn(300,300)*10**-6
myplot = plt.imshow(img)
class FixedOrderFormatter(mticker.ScalarFormatter):
# http://stackoverflow.com/questions/3677368/#3679918 (Joe Kington)
"""Formats axis ticks using scientific notation with a constant order of
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
img = np.random.randn(300,300)*10**-6
myplot = plt.imshow(img)
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
# x = a * 10**b = a * 10**(b-1) * 10**1
class MyInt(int):
def __add__(self, other):
print('__add__({}, {})'.format(self, other))
return MyInt(other+self)
def __radd__(self, other):
print('__radd__({}, {})'.format(self, other))
return MyInt(int(self)+other)
c = MyInt(5)
import errno
import os
import pty
import select
from subprocess import Popen, STDOUT
master_fd, slave_fd = pty.openpty() # provide tty to enable
# line-buffering on ruby's side
proc = Popen(['python', 'test.py'],
import warnings
import operator
import itertools as IT
import numpy as np
from numpy import nan
import pandas as pd
pd.options.display.width = 1000
pd.options.display.max_rows = 1000
def comparisons():
import matplotlib.pyplot as plt
import numpy as np
import timeit
def naive_power(m, n):
m = np.asarray(m)
res = m.copy()
for i in xrange(1,n):
res *= m
return res
import calendar
import pytz
import datetime as DT
tz1 = pytz.timezone('US/Eastern')
utc = pytz.timezone('UTC')
now = utc.localize(DT.datetime(2002, 10, 27, 7, 0, 0))
now_tz = now.astimezone(tz1)
now_epoch = calendar.timegm(now_tz.utctimetuple())
import calendar
import time
import pytz
import datetime as DT
utc = pytz.timezone('UTC')
for tzname in [name for name in pytz.all_timezones if 'Brazil' in name]:
tz1 = pytz.timezone(tzname)
date = utc.localize(DT.datetime(2013, 1, 1))
def memo(f):
"""Decorator that caches the return value for each call to f(args).
Then when called again with same args, we can just look it up."""
cache = {}
def _f(*args):
try:
return cache[args]
except KeyError: