i was playing around with the bcolz in memory compression examples from this notebook
So far i am really astonished by this library. And i think its a great tool for all of us that want to load bigger files into small memory (nice work Francesc, if you are reading this!)
I wonder if anyone has some experience in joining two ctables like with pandas.merge() and how to do this time/memory effective.
Thanks for sharing your ideas :-)!
I GOT IT JUST IN TIME.. big thanks to @mdurant for the itertoolz!! here's some pseudo-code as the example I was using is SUPER ugly.
# here's generic pandas
df_new = pd.merge(df1,df2)
# example with itertoolz and bcolz
from toolz.itertoolz import join as joinz
import bcolz
#convert them to ctables
zdf1 = bcolz.ctable.fromdataframe(df1)
zdf2 = bcolz.ctable.fromdataframe(df2)
#column 2 of df1 and column 1 of df2 were the columns to join on
merged = list(joinz(1,zdf1.iter(),0,zdf2.iter()))
# where new_dtypes are the dtypes of the fields you are using
# mine new_dtypes= '|S8,|S8,|S8,|S8,|S8'
zdf3 = bcolz.fromiter(((a[0]+a[1]) for a in merged), dtype = new_dtypes, count = len(merged))
obviously there are probably some smarter ways and this example isn't very specific , but it works and could serve as a base for someone to build it out more
#download movielens data files from http://grouplens.org/datasets/movielens/
#I'm using the 1M dataset
import pandas as pd
import time
from toolz.itertoolz import join as joinz
import bcolz
t0 = time()
dset = '/Path/To/Your/Data/'
udata = os.path.join(dset, 'users.dat')
u_cols = ['user_id', 'age', 'sex', 'occupation', 'zip_code']
users = pd.read_csv(udata,sep='::',names=u_cols)
rdata = os.path.join(dset, 'ratings.dat')
r_cols = ['user_id', 'movie_id', 'rating', 'unix_timestamp']
ratings = pd.read_csv(rdata, sep='::', names=r_cols)
print ("Time for parsing the data: %.2f" % (time()-t0,))
#Time for parsing the data: 4.72
t0=time()
users_ratings = pd.merge(users,ratings)
print ("Time for merging the data: %.2f" % (time()-t0,))
#Time for merging the data: 0.14
t0=time()
zratings = bcolz.ctable.fromdataframe(ratings)
zusers = bcolz.ctable.fromdataframe(users)
print ("Time for ctable conversion: %.2f" % (time()-t0,))
#Time for ctable conversion: 0.05
new_dtypes = ','.join([x[0].str for x in zusers.dtype.fields.values()][::-1] +[y[0].str for y in zratings.dtype.fields.values()][::-1])
#Do the merge with a list stored intermediately
t0 = time()
merged = list(joinz(0,zusers.iter(),0,zratings.iter()))
zuser_zrating1 = bcolz.fromiter(((a[0]+a[1]) for a in merged), dtype = new_dtypes, count = len(merged))
print ("Time for intermediate list bcolz merge: %.2f" % (time()-t0,))
#Time for intermediate list bcolz merge: 3.16
# Do the merge ONLY using iterators to limit memory consumption
t0 = time()
zuser_zrating2 = bcolz.fromiter(((a[0]+a[1]) for a in joinz(0,zusers.iter(),0,zratings.iter())) , dtype = new_dtypes, count = sum(1 for _ in joinz(0,zusers.iter(),0,zratings.iter())))
print ("Time for 2x iters of merged bcolz: %.2f" % (time()-t0,))
#Time for 2x iters of merged bcolz: 3.31
As you can see, the version I created is 15X slower than pandas, however by using only the iterators it will save A LOT of memory. Feel free to comment and/or expand on this. bcolz seems like a great package to build out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With