# AsNumpy fails with boolean branches

**URL:** <https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282>\
**Category:** ROOT\
**Tags:** bug, python, rdataframe\
**Created:** [December 20, 2019, 5:12pm UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282 "2019-12-20T17:12:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [December 20, 2019, 5:12pm UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282/1 "2019-12-20T17:12:29Z")

</div>

If I have an `RDataFrame` with a `Bool_t` column, `AsNumpy` fails:

```python
df.AsNumpy()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-e856f5516a02> in <module>()
----> 1 df.AsNumpy()

/Applications/root_build/lib/ROOT.pyc in _RDataFrameAsNumpy(df, columns, exclude)
    429 else:
    430 tmp = numpy.empty(len(cpp_reference), dtype=numpy.object)
--> 431 for i, x in enumerate(cpp_reference):
    432 tmp[i] = x # This creates only the wrapping of the objects and does not copy.
    433 py_arrays[column] = ndarray(tmp, result_ptrs[column])

AttributeError: 'vector<bool>' object has no attribute 'data'

```

Reproducer below.

* * *

First, we create the data frame with an integer column and a boolean column

```python
In [1]: import ROOT

In [2]: df = ROOT.RDataFrame(10).Define('e', 'rdfentry_').Define('b', 'rdfentry_ == 1')

```

Then we verify the types of the columns:

```python
In [6]: df.Snapshot('temp', 'temp.root')
Out[6]: <ROOT.ROOT::RDF::RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0x7fe77bdf3fb0>

In [7]: f = ROOT.TFile.Open('temp.root')

In [8]: t = f.Get('temp')

In [9]: for b in t.GetListOfBranches():
   ...: print b.GetName(), t.GetLeaf(b.GetName()).GetTypeName()
   ...:     
e ULong64_t
b Bool_t

```

Then we try to convert to numpy, and notice that it works with the `ULong64_t` column but not the `Bool_t` column:

```python
In [10]: df.AsNumpy()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-e856f5516a02> in <module>()
----> 1 df.AsNumpy()

/Applications/root_build/lib/ROOT.pyc in _RDataFrameAsNumpy(df, columns, exclude)
    429 else:
    430 tmp = numpy.empty(len(cpp_reference), dtype=numpy.object)
--> 431 for i, x in enumerate(cpp_reference):
    432 tmp[i] = x # This creates only the wrapping of the objects and does not copy.
    433 py_arrays[column] = ndarray(tmp, result_ptrs[column])

AttributeError: 'vector<bool>' object has no attribute 'data'

In [11]: df.AsNumpy(columns=['e'])
Out[11]: {'e': ndarray([0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L], dtype=object)}

```

* * *

_ROOT Version:_ master  
_Platform:_ macOS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [December 27, 2019, 6:09am UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282/2 "2019-12-27T06:09:23Z")

</div>

I went ahead and reported it as a JIRA bug [here](https://sft.its.cern.ch/jira/plugins/servlet/mobile#issue/ROOT-10491).

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [December 27, 2019, 1:34pm UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282/3 "2019-12-27T13:34:50Z")

</div>

in the meantime, you should be able to convert that ROOT file to numpy using [go-hep/root2npy](https://go-hep.org/x/hep/cmd/root2npy).

here are binaries for the latest (`v0.22.0` ATM) Go-HEP release:

- [https://go-hep.org/dist/v0.22.0/](https://go-hep.org/dist/v0.22.0/)

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [December 27, 2019, 3:16pm UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282/4 "2019-12-27T15:16:22Z")

</div>

There is also [`root_pandas`](https://github.com/scikit-hep/root_pandas) and the possibility of converting the column to an integer:

```python
df = df.Define(‘b_int’, ‘int(b)’)
df.AsNumpy(columns=[‘e’, ‘b_int’])

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 10, 2020, 3:16pm UTC](https://root-forum.cern.ch/t/asnumpy-fails-with-boolean-branches/37282/5 "2020-01-10T15:16:34Z")

</div>

Closed on January 10, 2020, 3:16pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
