admin管理员组文章数量:1122832
I am using numpy arrays extensively in my codebase. The different columns of the array relate to different data. I want to create a class that enforces some schema and allows custom methods to be added. I want to keep the methods and properties of the numpy array though.
I'm looking for something similar to a dataclass or pydantic object, that lets me keep the ability to index and interact with the the data the same as if it were a numpy array.
Here's what i've tried: Example 1
@dataclass
class MyCoord:
x: int
y: int
my_data = np.array([[0,0],[1,1]], dtype=MyCoord)
print(f"The y coords from my data {my_data[:,1]}, which is correct")
print(f"The y coord is not an attribute though {my_data[0].y}, will error")
I can subclass the numpy array, but thats not recommended for maintainability
本文标签: pythonHow to enforce a data schema on a numpy arrayStack Overflow
版权声明:本文标题:python - How to enforce a data schema on a numpy array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310474a1934388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论