question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numpy.array - wrong dtype for 'string enum'

See original GitHub issue

When 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:open
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alonmecommented, Mar 23, 2020

Not very familiar with the C-API but will be happy to get my hands dirty. Ill try and take a look.

1reaction
sebergcommented, Mar 17, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found