PyROOT中RDataFrame.AsNumpy()报错:np.object已废弃的解决咨询
问题:PyROOT RDataFrame转Numpy数组触发AttributeError(无需降级Numpy)
在Jupyter Notebook中使用PyROOT读取ROOT文件的TTree为RDataFrame,尝试将数据框列转换为Numpy数组时触发错误,已知降级Numpy可解决,但需要无需降级的方案。
完整代码
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()
数据框结构

报错信息
--------------------------------------------------------------------------- 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
无需降级Numpy的解决方案
1. 升级ROOT到兼容版本
你的ROOT版本是6.26.06,该版本的PyROOT代码仍在使用已被Numpy弃用的np.object。ROOT 6.28及以上版本已经修复了这个兼容性问题,升级ROOT后即可直接使用AsNumpy方法。
2. 手动修补PyROOT的pythonization文件
找到报错的_rdataframe.py文件(路径:/usr/local/Cellar/root/6.26.06_2/lib/root/ROOT/_pythonization/_rdataframe.py),打开后定位到第138行,将numpy.object替换为原生的object,保存文件后重启Jupyter Notebook即可。
3. 绕过AsNumpy,手动提取数据
使用RDataFrame的Take方法直接提取数据,再转换为Numpy数组,示例代码如下:
# 替换原代码中的a=df.AsNumpy(["Channel"]) # 注意:根据Channel分支的实际类型修改<>中的类型,比如int/float等 channel_list = df.Take["int"]("Channel") a = np.array(channel_list)
内容的提问来源于stack exchange,提问作者LuciaB
相关产品推荐
相关产品推荐

