Dtypes don't work like they look at first glance. np.uint16 isn't a dtype object. It's just convertible to one. np.uint16 is a type object representing the type of array scalars of uint16 dtype.

x.dtype is an actual dtype object, and dtype objects implement == in a weird way that's non-transitive and inconsistent with hash. dtype == other is basically implemented as dtype == np.dtype(other) when other isn't already a dtype. You can see the details in the source.

Particularly, x.dtype compares equal to np.uint16, but it doesn't have the same hash, so the dict lookup doesn't find it.

Answer from user2357112 on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › tagged › uint16
Newest 'uint16' Questions - Stack Overflow
If I have an array of floats and I try to convert it with array_uint16 = array_float64.astype(np.uint16) is there a sort of "...
🌐
Reddit
reddit.com › r/learnpython › declaring an unsigned integer 16 in python for bit shift operation
r/learnpython on Reddit: Declaring an Unsigned Integer 16 in Python for Bit Shift Operation
October 29, 2023 -

SOLVED: 3 Solutions:

  1. using Numpy : np.uint16()

  2. Using CTypes : ctypes.c_uint16()

  3. Using Bitwise : & 0xFFFF


Hi, I'm trying to convert this code to Python from Go / C. It involves declaring a UInt16 variable and run a bit shift operation. However cant seem to create a variable with this specific type. Need some advise here.

Go Code:

package main

import "fmt"

func main() {

var dx uint16 = 38629

var dy uint16 = dx << 8

fmt.Println(dy) //58624 -> Correct value

}

Python Code:

dx = 38629

dy = (dx << 8)

print(dy) # 9889024 -> not the expected value

print(type(dx)) # <class 'int'>

print(type(dy)) # <class 'int'>

I cant seem to figure out a way to cast or similar function to get this into an Unsigned Int 16.\

Please help.

Discussions

string - Typecasting in Python - Stack Overflow
I need to convert strings in Python to other types such as unsigned and signed 8, 16, 32, and 64 bit ints, doubles, floats, and strings. How can I do this? More on stackoverflow.com
🌐 stackoverflow.com
.net - How to use C# UInt16[,] in python - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Python How to convert the numpy array(dtype=uint16) to a string? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
April 20, 2020
python - how to present uint16 as float16 - Stack Overflow
I have a buffer of float16 elements in the memory. I read this buffer to a list but since I use np.ctypeslib I can't read it as float16. So I read it as uint16 and now I want to represent it as flo... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 72563282 › how-to-use-c-sharp-uint16-in-python
.net - How to use C# UInt16[,] in python - Stack Overflow
import clr import os import numpy as np dll_name = os.path.join(os.path.abspath(os.path.dirname(__file__)), ("mydll") + ".dll") clr.AddReference(dll_name) from mynamespace import myclass myobject = myclass() numpy_matrix = np.empty([80,260],dtype = np.uint16) SystemInt16_matrix = myobject.Getdata() for i in range(20): for j in range(32): numpy_matrix[i,j]=SystemInt16_matrix[i,j]
Find elsewhere
🌐
KooR.fr
koor.fr › Python › API › scientist › numpy › uint16 › Index.wp
KooR.fr - classe uint16 - module numpy - Description de quelques librairies Python
bool broadcast busdaycalendar bytes_ character clongdouble complex128 complex64 complexfloating datetime64 dtype errstate finfo flatiter flexible float16 float32 float64 floating generic iinfo inexact int16 int32 int64 int8 intc integer longdouble matrix memmap ndarray ndenumerate ndindex nditer number object_ poly1d recarray record signedinteger str_ timedelta64 ufunc uint16 uint32 uint64 uint8 uintc unsignedinteger vectorize void
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
Notice the main difference: in C, the data types of each variable are explicitly declared, while in Python the types are dynamically inferred. This means, for example, that we can assign any kind of data to any variable: ... Here we've switched the contents of x from an integer to a string.
🌐
Python Examples
pythonexamples.org › python-convert-int-to-string
Convert Int to String
#take an int a = 5 print('Input', a, type(a), sep='\n') #convert int to string output = str(a) print('\nOutput', output, type(output), sep='\n') ... In this tutorial of Python Examples, we learned to convert a value of int datatype to a value of string datatype using str() function.
🌐
Readthedocs
iec104-python.readthedocs.io › latest › python › number › uint16.html
Usigned Integer (16-bit) — iec104-python 2.2 documentation
c104 python module · Number · Usigned Integer (16-bit) View page source · class c104.UInt16 · __init__(self, value: int) → None · create a fixed-length integer instance · Parameters: value (int) – the value · Raises: ValueError – cannot convert value to fixed-length integer ·
🌐
GitHub
github.com › pandas-dev › pandas › issues › 47294
BUG: uint16 inserted as int16 when assigning row with dict · Issue #47294 · pandas-dev/pandas
June 9, 2022 - It's expected the dtype is preserved - uint16 if possible, or an int which is large enough to represent the value. python : 3.8.10.final.0 python-bits : 64 OS : Linux machine : x86_64 pandas : 1.4.2 numpy : 1.22.4 pytz : 2022.1 dateutil : 2.8.2 pip : 20.0.2 setuptools : 44.0.0 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : None pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None markupsafe : 2.1.1 matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None
Author   bluss