Cannot use DataFrame.AsNumpy() : np.object deprecated error

90 Views Asked by At

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!

0

There are 0 best solutions below