Skip to content

Instantly share code, notes, and snippets.

@kaotika
Last active August 29, 2015 14:13
Show Gist options
  • Save kaotika/1938d80e886920d6ef30 to your computer and use it in GitHub Desktop.
Save kaotika/1938d80e886920d6ef30 to your computer and use it in GitHub Desktop.
Pandas apply+map comparison
import numpy as np
import pandas as pd
import timeit
import geohash
samples = 1000
precision = 22
points = np.random.rand(samples, 2)
data = pd.DataFrame(points, columns=['lat', 'lng'])
data.lat += 13
data.lng += 52
data2 = data.copy()
def test1():
data['test'] = data.apply(lambda x: geohash.encode(x['lat'], x['lng'], precision=precision), axis=1)
def test3():
data['_tmp1'] = zip(data.lat, data.lng)
data['test'] = data._tmp1.map(lambda x: geohash.encode(x[0], x[1], precision=precision))
#data.drop('_tmp1', 1)
del data['_tmp1']
def test4():
data['_tmp1'] = zip(data.lat, data.lng)
data['test'] = data._tmp1.apply(lambda x: geohash.encode(x[0], x[1], precision=precision))
del data['_tmp1']
def test2():
for index, row in data2.iterrows():
geohash.encode(row['lat'], row['lng'], precision=precision)
iterations = 10
print timeit.timeit('test1()', setup="from __main__ import test1", number=iterations) / iterations * 1000
print timeit.timeit('test2()', setup="from __main__ import test2", number=iterations) / iterations * 1000
print timeit.timeit('test3()', setup="from __main__ import test3", number=iterations) / iterations * 1000
print timeit.timeit('test4()', setup="from __main__ import test4", number=iterations) / iterations * 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment