I am trying to select random rows from an HDFStore table of about 1 GB. RAM usage explodes when I request about 50 random strings.
I am using pandas 0-11-dev, python 2.7, linux64 .
In this first case, using RAM matches the size of the chunk
with pd.get_store("train.h5",'r') as train: for chunk in train.select('train',chunksize=50): pass
In this second case, it seems that the entire table is loaded into RAM
r=random.choice(400000,size=40,replace=False) train.select('train',pd.Term("index",r))
In this latter case, the use of RAM corresponds to the equivalent chunk size
r=random.choice(400000,size=30,replace=False) train.select('train',pd.Term("index",r))
I am puzzled why moving from 30 to 40 random strings causes such a sharp increase in RAM usage.
Note that the table was indexed at creation so that index = range (nrows (table)) using the following code:
def txtfile2hdfstore(infile, storefile, table_name, sep="\t", header=0, chunksize=50000 ): max_len, dtypes0 = txtfile2dtypes(infile, sep, header, chunksize) with pd.get_store( storefile,'w') as store: for i, chunk in enumerate(pd.read_table(infile,header=header,sep=sep,chunksize=chunksize, dtype=dict(dtypes0))): chunk.index= range( chunksize*(i), chunksize*(i+1))[:chunk.shape[0]] store.append(table_name,chunk, min_itemsize={'values':max_len})
Thank you for understanding
EDIT ANSWER Zelazny7
Here is the file I used to write Train.csv to train.h5. I wrote this using the Zelazny7 code elements from How to eliminate the HDFStore exception: cannot find the correct atom type
import pandas as pd import numpy as np from sklearn.feature_extraction import DictVectorizer def object_max_len(x): if x.dtype != 'object': return else: return len(max(x.fillna(''), key=lambda x: len(str(x)))) def txtfile2dtypes(infile, sep="\t", header=0, chunksize=50000 ): max_len = pd.read_table(infile,header=header, sep=sep,nrows=5).apply( object_max_len).max() dtypes0 = pd.read_table(infile,header=header, sep=sep,nrows=5).dtypes for chunk in pd.read_table(infile,header=header, sep=sep, chunksize=chunksize): max_len = max((pd.DataFrame(chunk.apply( object_max_len)).max(),max_len)) for i,k in enumerate(zip( dtypes0[:], chunk.dtypes)): if (k[0] != k[1]) and (k[1] == 'object'): dtypes0[i] = k[1]
Used as
txtfile2hdfstore('Train.csv','train.h5','train',sep=',')