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

Commit fb29e49

Browse files
authored
Merge pull request #67 from chrisburr/fix-60
Fix #60
2 parents f118e40 + ba1cbf0 commit fb29e49

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

root_pandas/readwrite.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,19 @@ def read_root(paths, key=None, columns=None, ignore=None, chunksize=None, where=
199199

200200
if not isinstance(paths, list):
201201
paths = [paths]
202-
# Use a single file to search for trees and branches
203-
seed_path = paths[0]
202+
# Use a single file to search for trees and branches, ensuring the key exists
203+
for seed_path in paths:
204+
trees = list_trees(seed_path)
205+
if key and key not in trees:
206+
continue
207+
break
208+
else:
209+
if key:
210+
raise OSError('{} not found in any of the given paths'.format(key))
211+
else:
212+
raise OSError('No trees found in any of the given paths')
204213

205214
if not key:
206-
trees = list_trees(seed_path)
207215
if len(trees) == 1:
208216
key = trees[0]
209217
elif len(trees) == 0:

tests/test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from root_pandas import read_root
2+
from root_pandas import read_root, to_root
33
from root_numpy import list_branches
44
from root_numpy import array2root
55
from pandas.util.testing import assert_frame_equal
@@ -311,3 +311,13 @@ def test_brace_pattern_in_columns():
311311
reference_df[['var02', 'var03', 'var11', 'var13']])
312312

313313
os.remove('tmp.root')
314+
315+
316+
def test_detect_branches_first_missing():
317+
df = pd.DataFrame({'a': list(range(10)), 'b': list(range(10))})
318+
to_root(df, 'tmp_1.root', 'my_tree_1')
319+
to_root(df, 'tmp_2.root', 'my_tree')
320+
read_df = read_root(['tmp_1.root', 'tmp_2.root'], 'my_tree', warn_missing_tree=True)
321+
assert_frame_equal(df, read_df)
322+
os.remove('tmp_1.root')
323+
os.remove('tmp_2.root')

tests/test_issues.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import root_pandas
55

66

7+
def test_issue_60():
8+
df = pd.DataFrame({'a': list(range(10)), 'b': list(range(10))})
9+
root_pandas.to_root(df, 'tmp_1.root', 'my_tree_1')
10+
root_pandas.to_root(df, 'tmp_2.root', 'my_tree')
11+
result = root_pandas.read_root(['tmp_1.root', 'tmp_2.root'], 'my_tree', warn_missing_tree=True)
12+
assert len(result) == 10
13+
os.remove('tmp_1.root')
14+
os.remove('tmp_2.root')
15+
16+
717
def test_issue_63():
818
df = pd.DataFrame({'a': [], 'b': []})
919
root_pandas.to_root(df, 'tmp_1.root', 'my_tree')

0 commit comments

Comments
 (0)