Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

add __len__ method to read_root with chunks #75

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions root_pandas/readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,25 @@ def read_root(paths, key=None, columns=None, ignore=None, chunksize=None, where=
for path in paths:
tchain.Add(path)
n_entries = tchain.GetEntries()
n_chunks = int(ceil(float(n_entries) / chunksize))
# XXX could explicitly clean up the opened TFiles with TChain::Reset

def genchunks():
current_index = 0
for chunk in range(int(ceil(float(n_entries) / chunksize))):
arr = root2array(paths, key, all_vars, start=chunk * chunksize, stop=(chunk+1) * chunksize, selection=where, *args, **kwargs)
if len(arr) == 0:
continue
if flatten:
arr = do_flatten(arr, flatten)
yield convert_to_dataframe(arr, start_index=current_index)
current_index += len(arr)
return genchunks()
class genchunk(object):
def __len__(self):
return n_chunks

def __iter__(self):
current_index = 0
for chunk in range(n_chunks):
arr = root2array(paths, key, all_vars, start=chunk * chunksize, stop=(chunk+1) * chunksize, selection=where, *args, **kwargs)
if len(arr) == 0:
continue
if flatten:
arr = do_flatten(arr, flatten)
yield convert_to_dataframe(arr, start_index=current_index)
current_index += len(arr)

return genchunk()

arr = root2array(paths, key, all_vars, selection=where, *args, **kwargs)
if flatten:
Expand Down