Skip to content

Create a cast method for Arrays? #291

@scott-griffiths

Description

@scott-griffiths

Changing the dtype of an Array just changes the interpretation of the underlying data. This is fine, and is a O(1) operation which fits with changing a property, but some users might want or expect it to recast the data to the new type.

To cast to a new dtype you need to do this:

a = Array('u8', [1, 2, 3, 4, 5, 6, 7, 8])

b = Array('float64', a.tolist())

which is OK, and explicit, but adding a new method could make it clearer and give more options:

b = a.cast('float64')

I don't think it's good to do it in place - there's no performance gain. We can now also deal with things like overflows better:

c = b.cast('u16', clip=True)

so the user can choose whether to get a ValueError or to clip values or whatever (divide by zero would be another one).

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions