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'],
@unutbu
unutbu / gist:7534865
Created November 18, 2013 20:36
finite differences
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
#Data:
x = np.linspace(0, 2*np.pi,100)
f = np.sin(x) + 0.002*(np.random.rand(100)-.5)
#Normalization:
dx = x[1]-x[0]
Invoked with :
--ncalls: 3
--repeats: 6
-------------------------------------------------------------------------------
Test name | head[ms] | base[ms] | ratio |
-------------------------------------------------------------------------------
groupby_first_float32 | 5.1910 | 6.4894 | 0.7999 |
Invoked with :
--ncalls: 3
--repeats: 6
-------------------------------------------------------------------------------
Test name | head[ms] | base[ms] | ratio |
-------------------------------------------------------------------------------
mask_bools | 33.1504 | 46.0804 | 0.7194 |
Invoked with :
--ncalls: 3
--repeats: 3
-------------------------------------------------------------------------------
Test name | head[ms] | base[ms] | ratio |
-------------------------------------------------------------------------------
frame_reindex_columns | 0.4276 | 0.5569 | 0.7678 |
import numpy as np
import pandas as pd
import guppy
def array_equivalent(a1, a2):
h.setrelheap()
try:
a1, a2 = np.asarray(a1), np.asarray(a2)
except (TypeError, ValueError):
return False