如何将NumPy数据类型转换为自定义数据类型?

时间:2022-03-18 16:33:41

I have defined a new numeric data type in Python using Python's Data Model. I would like to convert all my existing NumPy arrays from their existing data types to my custom data type. I understand that NumPy's astype method converts from one data type to another, but based on my understanding, it can only convert between built-in data types.

我使用Python的数据模型在Python中定义了一个新的数值数据类型。我想将现有的NumPy数组从现有数据类型转换为自定义数据类型。我知道NumPy的astype方法从一种数据类型转换为另一种数据类型,但根据我的理解,它只能在内置数据类型之间进行转换。

In contrast to the answer provided here, my data type is not based on built-in data types and has it's own addition, multiplication, bit-wise operations, etc., so I cannot use np.dtype to define my data type. In other words, the following solution would not work:

与此处提供的答案相反,我的数据类型不是基于内置数据类型,并且有自己的加法,乘法,逐位运算等,所以我不能使用np.dtype来定义我的数据类型。换句话说,以下解决方案不起作用:

kerneldt = np.dtype([('myintname', np.int32), ('myfloats', np.float64, 9)])
arr = np.empty(dims, dtype=kerneldt)

Is there any way to convert between a built-in data type and a custom data type and vice versa?

有没有办法在内置数据类型和自定义数据类型之间进行转换,反之亦然?

1 个解决方案

#1


1  

This isn't currently possible. There are plans to allow custom dtypes in numpy in the future.

目前这不可行。有计划在未来允许自定义dtypes numpy。

#1


1  

This isn't currently possible. There are plans to allow custom dtypes in numpy in the future.

目前这不可行。有计划在未来允许自定义dtypes numpy。