Numpy.array - wrong dtype for 'string enum'
See original GitHub issueWhen creating a numpy.array
of a class that inherits from both str and Enum the dtype is dtype='<UX
where X is the length of the string. the data saved is X chars from the string representation of the enum.
I would expect the minimal value to be object, so the Enum is preserved.
Problem is of course solved when passing object
as dtype
Reproducing code example:
>>> import numpy as np
>>> from enum import Enum
>>> class StrEnum(str, Enum):
... A = 'a'
... B = 'bb'
...
>>> np.array([StrEnum.A])
array(['S'], dtype='<U1')
>>> np.array([StrEnum.B])
array(['St'], dtype='<U2')
Numpy/Python version information:
1.18.0 3.7.5 (default, Nov 1 2019, 02:16:23) [Clang 11.0.0 (clang-1100.0.33.8)]
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
changing the dtype of a numpy array to something custom
My custom class inherits from Enum . I have a class for the values of playing cards: from enum import Enum from functools...
Read more >Array API — NumPy v1.25.dev0 Manual
Construct a one-dimensional ndarray of a single type from a binary or (ASCII) text string of length slen . The data-type of the...
Read more >creating a numpy array with values to be cast to an enum?
I'm trying to create an 5xN array of values to pass from a function. One of these values is actually an enumeration, so...
Read more >Source code for Corrfunc.utils
#!/usr/bin/env python # -*- coding: utf-8 -*- """ A set of utility ... 10.0 try: input_dtype = cz.dtype except: msg = "Input cz...
Read more >After upgrade to the latest version now this error id showing up ...
ArrowInvalid: ('Could not convert int64 with type numpy.dtype: did not ... File “pyarrow\array.pxi”, line 301, in pyarrow.lib.array
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not very familiar with the C-API but will be happy to get my hands dirty. Ill try and take a look.
We currently also coerce subclasses of types that NumPy knows about to store them into arrays. I agree with it being bad behaviour, in the long run, I would like to deprecate it.