-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pandas and double nested vectors issue 885 (#912)
* fix: pandas and double nested vectors issue 885 * style: pre-commit fixes * Rename test file to match PR number. * Fix ROOT import. * Fix order of operations. * Fix order of operations.2 * style: pre-commit fixes * Change the logic in pandas finalize. * style: pre-commit fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b96ae44
commit a8644df
Showing
4 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
tests/test_0912-fix-pandas-and-double-nested-vectors-issue-885.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE | ||
|
||
import uproot | ||
import pytest | ||
import skhep_testdata, os | ||
import numpy as np | ||
import awkward as ak | ||
|
||
ROOT = pytest.importorskip("ROOT") | ||
|
||
|
||
def test_pandas_and_double_nested_vectors_issue_885(tmp_path): | ||
filename = os.path.join( | ||
tmp_path, "uproot_test_pandas_and_double_nested_vectors.root" | ||
) | ||
f = ROOT.TFile(filename, "recreate") | ||
t = ROOT.TTree("mytree", "example tree") | ||
|
||
vec1 = ROOT.std.vector("double")() | ||
vec2 = ROOT.std.vector("double")() | ||
vec_vec = ROOT.std.vector(ROOT.std.vector("double"))() | ||
|
||
for i in range(3): | ||
vec1.push_back(i) | ||
for i in range(5): | ||
vec2.push_back(i) | ||
|
||
vec_vec.push_back(vec1) | ||
vec_vec.push_back(vec2) | ||
|
||
a = np.array([1, 2, 3, 4], dtype=np.uint32) | ||
avec = ROOT.std.vector("unsigned int")(a) | ||
|
||
b = np.array([[[0, 1, 3], [4, 5, 6], [7, 8, 9]]], dtype=np.uint32) | ||
bvec = ROOT.std.vector("unsigned int")(b) | ||
|
||
t.Branch("2Dvector", vec_vec) | ||
t.Branch("1Dvector", avec) | ||
t.Branch("othervector", bvec) | ||
|
||
nentries = 25 | ||
for i in range(nentries): | ||
t.Fill() | ||
|
||
f.Write() | ||
|
||
with uproot.open(filename)["mytree"] as fs: | ||
u = fs.arrays(["2Dvector", "1Dvector", "othervector"], library="pd") | ||
assert isinstance(u["2Dvector"][0], ak.highlevel.Array) | ||
assert isinstance(u["1Dvector"][0], ak.highlevel.Array) | ||
assert isinstance(u["othervector"][0], ak.highlevel.Array) | ||
assert ak.to_list(u["2Dvector"][0]) == [[0, 1, 2], [0, 1, 2, 3, 4]] | ||
assert ak.to_list(u["1Dvector"][0]) == [1, 2, 3, 4] | ||
assert ak.to_list(u["othervector"][0]) == [0, 1, 3, 4, 5, 6, 7, 8, 9] |