0

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

4
  • "The different columns of the array relate to different data" This is generally not a good idea performance-wise. You should use multiple arrays to better benefits from SIMD instruction boost. See AoS vs SoA. Commented Nov 21, 2024 at 14:40
  • 1
    Sounds a bit like you want a structured array. Computationally it has few benefits compared to separate arrays. As written your array is object dtype. Commented Nov 21, 2024 at 17:01
  • perhaps the pandas package would be helpful? Commented Nov 22, 2024 at 1:19
  • structured arrays are a step towards my objective, and i looked into them. I'm looking to enforce stricter rules. i.e. column 1 must have values between 0-1, column 2 must have positive integers etc. Structured arrays can enforce broad types but i don't see a way to enforce those rules. I'm not worried about performance concerns, but thank you @JérômeRichard for the link. Pandas also doesn't enforce the schema either. In the end i've created a dataclass with numpy array as the data store, then added len and get_item methods to support numpy-like indexing. Commented Dec 19, 2024 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.