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