0

I am using PyRoot on Jupyter notebook.I have TTree in a ROOT file, which I read as an RDataFrame. I am trying to convert columns of my dataframe (branches of the original TTree) into Numpy arrays, but the below error is raised. My complete code is:

import ROOT
import sys
import numpy as np
import matplotlib.pyplot as plt
ROOT.ROOT.EnableImplicitMT()
%jsroot on
### INPUT STYLE 
runNumber = input()
n=33

while(runNumber != "0"):
    print(runNumber)
    fileName =path+f'run_{runNumber}/RAW/SDataR_run_{runNumber}.root'
    df = ROOT.RDataFrame("Data_R;", fileName)
    a=df.AsNumpy(["Channel"])
    print("Channel array: ", a)
    print("")
    print("Enter another run number, or '0' to exit")
    runNumber = input()`

To get an idea fo the dataframe below is its structure:

Dataframe

and this is the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[48], line 1
----> 1 a=df.AsNumpy(["Channel"])

File /usr/local/Cellar/root/6.26.06_2/lib/root/ROOT/_pythonization/_rdataframe.py:87, in RDataFrameAsNumpy(df, columns, exclude, lazy)
     85     return result
     86 else:
---> 87     return result.GetValue()

File /usr/local/Cellar/root/6.26.06_2/lib/root/ROOT/_pythonization/_rdataframe.py:138, in AsNumpyResult.GetValue(self)
    136     self._py_arrays[column] = ndarray(tmp, self._result_ptrs[column])
    137 else:
--> 138     tmp = numpy.empty(len(cpp_reference), dtype=numpy.object)
    139     for i, x in enumerate(cpp_reference):
    140         tmp[i] = x # This creates only the wrapping of the objects and does not copy.

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py:305, in __getattr__(attr)
    300     warnings.warn(
    301         f"In the future `np.{attr}` will be defined as the "
    302         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    304 if attr in __former_attrs__:
--> 305     raise AttributeError(__former_attrs__[attr])
    307 # Importing Tester requires importing all of UnitTest which is not a
    308 # cheap import Since it is mainly used in test suits, we lazy import it
    309 # here to save on the order of 10 ms of import time for most users
    310 #
    311 # The previous way Tester was imported also had a side effect of adding
    312 # the full `numpy.testing` namespace
    313 if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. 
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
```

I am not sure how to solve this issue. I have read it might be solved by downgrading Numpy, but maybe you have some other suggestions that avoid the downgrade. Many thanks!

LuciaB
  • 11
  • 1
  • Could you add the complete code please, not just the one line command you added. It would also be nice to see part of your dataframe. Thanks! – Linux Jul 26 '23 at 08:42
  • Yes, sorry! I have edited my original question with firther details! Thank you. – LuciaB Jul 26 '23 at 09:10
  • Please add all `import` lines to know the specific libraries you use and any definitions of user-defined methods. `RDataFrame`? – Parfait Jul 26 '23 at 10:53
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Jul 26 '23 at 13:59
  • Thank you, I have edited leaving only the small part of code giving problems, and adding the input. What I have is not a pandas dataframe. I originally have a ROOT TTree, which I read as RDataFrame (https://root.cern/doc/master/classROOT_1_1RDataFrame.html#RDFCollections). I want to convert the "Channel" collection of the DataFrame (which was originally a branch of my TTree) into a numpy array. I have found online many examples simply using AsNumpy(), but this raises in my case the error in my question. – LuciaB Jul 26 '23 at 16:24
  • According to pypi, PYROOT latest release is May 2022. You may have to revert your numpy to a release from about that time. Newer numpy's have removed a number of depricated things like `np.object`, expecting the code to simply use `np.empty(..., dtype=object)`. – hpaulj Jul 26 '23 at 18:16

0 Answers0