\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"
| Type | Type Code |
|---|
| 0 | int8 | i1 |
|---|
| 1 | uint8 | u1 |
|---|
| 2 | int16 | i2 |
|---|
| 3 | uint16 | u2 |
|---|
| 4 | int or int32 | i4 or i |
|---|
| 5 | uint32 | u4 |
|---|
| 6 | int64 | i8 |
|---|
| 7 | uint64 | u8 |
|---|
| 8 | float16 | f2 |
|---|
| 9 | float32 | f4 or f |
|---|
| 10 | float or float64 | f8 or d |
|---|
| 11 | float128 | f16 or g |
|---|
| 12 | complex64 | c8 |
|---|
| 13 | complex or complex128 | c16 |
|---|
| 14 | bool | None |
|---|
| 15 | object | O |
|---|
| 16 | string_ | S |
|---|
| 17 | unicode_ | U |
|---|
\n",
"
"
],
"text/plain": [
" Type Type Code\n",
"0 int8 i1\n",
"1 uint8 u1\n",
"2 int16 i2\n",
"3 uint16 u2\n",
"4 int or int32 i4 or i\n",
"5 uint32 u4\n",
"6 int64 i8\n",
"7 uint64 u8\n",
"8 float16 f2\n",
"9 float32 f4 or f\n",
"10 float or float64 f8 or d\n",
"11 float128 f16 or g\n",
"12 complex64 c8\n",
"13 complex or complex128 c16\n",
"14 bool None\n",
"15 object O\n",
"16 string_ S\n",
"17 unicode_ U"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dtypes = pd.DataFrame(\n",
" {\n",
" 'Type': [\n",
" 'int8', \n",
" 'uint8', \n",
" 'int16', \n",
" 'uint16', \n",
" 'int or int32', \n",
" 'uint32', \n",
" 'int64', \n",
" 'uint64', \n",
" 'float16', \n",
" 'float32', \n",
" 'float or float64',\n",
" 'float128', \n",
" 'complex64', \n",
" 'complex or complex128', \n",
" 'bool', \n",
" 'object', \n",
" 'string_',\n",
" 'unicode_',\n",
" ],\n",
" \n",
" 'Type Code': [\n",
" 'i1', \n",
" 'u1', \n",
" 'i2', \n",
" 'u2', \n",
" 'i4 or i', \n",
" 'u4', \n",
" 'i8', \n",
" 'u8', \n",
" 'f2', \n",
" 'f4 or f', \n",
" 'f8 or d', \n",
" 'f16 or g', \n",
" 'c8', \n",
" 'c16', \n",
" None, \n",
" 'O', \n",
" 'S', \n",
" 'U',\n",
" ]\n",
" }\n",
")\n",
"\n",
"dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data types can be defined at creating the numpy array and converted to other types later. \n",
"\n",
"You can use either _type, type code_ or _```np``` dot_ methods to define the data type of an array, but when you use ```np``` dot method to define the data type, it can only follow _type_ rather than _type code_."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float32')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([1,2,3], dtype='f4')\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float32')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Identical to the above\n",
"arr = np.array([1,2,3], dtype='float32')\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('complex64')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([1+2j, 3-4j], dtype=np.complex64)\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('complex64')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Identical to the above\n",
"arr = np.array([1+2j, 3-4j], dtype='c8')\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'numpy' has no attribute 'c8'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m