`.dtype.type` class object is not preserved over some operations
See original GitHub issueimport numpy as np
for dt in (np.uint8, np.uint16, np.uint32, np.uint64,
np.int8, np.int16, np.int32, np.int64,
np.float16, np.float32, np.float64):
x = np.array([0, 1], dtype=dt)
if np.issubdtype(dt, np.floating):
y = np.finfo(dt).max - x
else:
y = np.iinfo(dt).max - x
id_x = hex(id(x.dtype.type))
id_y = hex(id(y.dtype.type))
print('{}: {}, {}, {}'.format(dt, id_x, id_y, id_x == id_y))
<class 'numpy.uint8'>: 0x7fa0e529f740, 0x7fa0e529f740, True
<class 'numpy.uint16'>: 0x7fa0e529f5a0, 0x7fa0e529f5a0, True
<class 'numpy.uint32'>: 0x7fa0e529f400, 0x7fa0e529f400, True
<class 'numpy.uint64'>: 0x7fa0e529f260, 0x7fa0e529f0c0, False
<class 'numpy.int8'>: 0x7fa0e529ff60, 0x7fa0e529ff60, True
<class 'numpy.int16'>: 0x7fa0e529fdc0, 0x7fa0e529fdc0, True
<class 'numpy.int32'>: 0x7fa0e529fc20, 0x7fa0e529fc20, True
<class 'numpy.int64'>: 0x7fa0e529fa80, 0x7fa0e529f8e0, False
<class 'numpy.float16'>: 0x7fa0e529ef20, 0x7fa0e529ef20, True
<class 'numpy.float32'>: 0x7fa0e529ed80, 0x7fa0e529ed80, True
<class 'numpy.float64'>: 0x7fa0e529ebe0, 0x7fa0e529ebe0, True
This could lead to some issues in users code heavily relying on dtype.type comparison (e.g. https://github.com/scikit-image/scikit-image/issues/3043).
Linux, x64, numpy 1.14.2.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
What does dtype=object mean while creating a numpy array?
NumPy arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) ...
Read more >Data type Object (dtype) in NumPy Python - GeeksforGeeks
Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using...
Read more >Why We Need to Use Pandas New String Dtype Instead of ...
Select_dtypes(include=”object”) will return any column with object data type. On the other hand, if we use “string” data type for textual data, ...
Read more >Categorical Data — pandas 0.16.1 documentation - PyData |
If the slicing operation returns either a DataFrame or a column of type Series , the category dtype is preserved. In [107]: idx...
Read more >NEP 42 — New and extensible DTypes - NumPy
array operations may or may not preserve dtype metadata ... class DType(np.dtype): type : type # Python scalar type parametric : bool #...
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
Yeah, the bug is that it falls to the unsigned path, and that fallback does not convert the original typenum to its unsigned version, but instead always uses ulonglong. If you use
.min
instead of max, it works 😉.Just fired up WSL with numpy 1.11 (!), and I think I can pinpoint the bug:
In my opinion, we should swap between these which one passes and which fails.
We’re being inconsistent about when we pick
long
and when we picklong long
for int64 on linux. By name,long
takes precedence - but it seems that when assigning dtypes to scalars based on their value, we preferlong long
.On windows, it seems we handle
int
vslong
forint32
consistently.