|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import numpy as np" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### A list of Numpy Data Types" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 2, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [ |
| 24 | + { |
| 25 | + "data": { |
| 26 | + "text/html": [ |
| 27 | + "<div>\n", |
| 28 | + "<style scoped>\n", |
| 29 | + " .dataframe tbody tr th:only-of-type {\n", |
| 30 | + " vertical-align: middle;\n", |
| 31 | + " }\n", |
| 32 | + "\n", |
| 33 | + " .dataframe tbody tr th {\n", |
| 34 | + " vertical-align: top;\n", |
| 35 | + " }\n", |
| 36 | + "\n", |
| 37 | + " .dataframe thead th {\n", |
| 38 | + " text-align: right;\n", |
| 39 | + " }\n", |
| 40 | + "</style>\n", |
| 41 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 42 | + " <thead>\n", |
| 43 | + " <tr style=\"text-align: right;\">\n", |
| 44 | + " <th></th>\n", |
| 45 | + " <th>Type</th>\n", |
| 46 | + " <th>Type Code</th>\n", |
| 47 | + " </tr>\n", |
| 48 | + " </thead>\n", |
| 49 | + " <tbody>\n", |
| 50 | + " <tr>\n", |
| 51 | + " <th>0</th>\n", |
| 52 | + " <td>int8</td>\n", |
| 53 | + " <td>i1</td>\n", |
| 54 | + " </tr>\n", |
| 55 | + " <tr>\n", |
| 56 | + " <th>1</th>\n", |
| 57 | + " <td>uint8</td>\n", |
| 58 | + " <td>u1</td>\n", |
| 59 | + " </tr>\n", |
| 60 | + " <tr>\n", |
| 61 | + " <th>2</th>\n", |
| 62 | + " <td>int16</td>\n", |
| 63 | + " <td>i2</td>\n", |
| 64 | + " </tr>\n", |
| 65 | + " <tr>\n", |
| 66 | + " <th>3</th>\n", |
| 67 | + " <td>uint16</td>\n", |
| 68 | + " <td>u2</td>\n", |
| 69 | + " </tr>\n", |
| 70 | + " <tr>\n", |
| 71 | + " <th>4</th>\n", |
| 72 | + " <td>int32</td>\n", |
| 73 | + " <td>i4</td>\n", |
| 74 | + " </tr>\n", |
| 75 | + " <tr>\n", |
| 76 | + " <th>5</th>\n", |
| 77 | + " <td>uint32</td>\n", |
| 78 | + " <td>u4</td>\n", |
| 79 | + " </tr>\n", |
| 80 | + " <tr>\n", |
| 81 | + " <th>6</th>\n", |
| 82 | + " <td>int64</td>\n", |
| 83 | + " <td>i8</td>\n", |
| 84 | + " </tr>\n", |
| 85 | + " <tr>\n", |
| 86 | + " <th>7</th>\n", |
| 87 | + " <td>uint64</td>\n", |
| 88 | + " <td>u8</td>\n", |
| 89 | + " </tr>\n", |
| 90 | + " <tr>\n", |
| 91 | + " <th>8</th>\n", |
| 92 | + " <td>float16</td>\n", |
| 93 | + " <td>f2</td>\n", |
| 94 | + " </tr>\n", |
| 95 | + " <tr>\n", |
| 96 | + " <th>9</th>\n", |
| 97 | + " <td>float32</td>\n", |
| 98 | + " <td>f4 or f</td>\n", |
| 99 | + " </tr>\n", |
| 100 | + " <tr>\n", |
| 101 | + " <th>10</th>\n", |
| 102 | + " <td>float64</td>\n", |
| 103 | + " <td>f8 or d</td>\n", |
| 104 | + " </tr>\n", |
| 105 | + " <tr>\n", |
| 106 | + " <th>11</th>\n", |
| 107 | + " <td>float128</td>\n", |
| 108 | + " <td>f16 or g</td>\n", |
| 109 | + " </tr>\n", |
| 110 | + " <tr>\n", |
| 111 | + " <th>12</th>\n", |
| 112 | + " <td>complex64</td>\n", |
| 113 | + " <td>c8</td>\n", |
| 114 | + " </tr>\n", |
| 115 | + " <tr>\n", |
| 116 | + " <th>13</th>\n", |
| 117 | + " <td>complex128</td>\n", |
| 118 | + " <td>c16</td>\n", |
| 119 | + " </tr>\n", |
| 120 | + " <tr>\n", |
| 121 | + " <th>14</th>\n", |
| 122 | + " <td>bool</td>\n", |
| 123 | + " <td></td>\n", |
| 124 | + " </tr>\n", |
| 125 | + " <tr>\n", |
| 126 | + " <th>15</th>\n", |
| 127 | + " <td>object</td>\n", |
| 128 | + " <td>O</td>\n", |
| 129 | + " </tr>\n", |
| 130 | + " <tr>\n", |
| 131 | + " <th>16</th>\n", |
| 132 | + " <td>string_</td>\n", |
| 133 | + " <td>S</td>\n", |
| 134 | + " </tr>\n", |
| 135 | + " <tr>\n", |
| 136 | + " <th>17</th>\n", |
| 137 | + " <td>unicode_</td>\n", |
| 138 | + " <td>U</td>\n", |
| 139 | + " </tr>\n", |
| 140 | + " </tbody>\n", |
| 141 | + "</table>\n", |
| 142 | + "</div>" |
| 143 | + ], |
| 144 | + "text/plain": [ |
| 145 | + " Type Type Code\n", |
| 146 | + "0 int8 i1\n", |
| 147 | + "1 uint8 u1\n", |
| 148 | + "2 int16 i2\n", |
| 149 | + "3 uint16 u2\n", |
| 150 | + "4 int32 i4\n", |
| 151 | + "5 uint32 u4\n", |
| 152 | + "6 int64 i8\n", |
| 153 | + "7 uint64 u8\n", |
| 154 | + "8 float16 f2\n", |
| 155 | + "9 float32 f4 or f\n", |
| 156 | + "10 float64 f8 or d\n", |
| 157 | + "11 float128 f16 or g\n", |
| 158 | + "12 complex64 c8\n", |
| 159 | + "13 complex128 c16\n", |
| 160 | + "14 bool \n", |
| 161 | + "15 object O\n", |
| 162 | + "16 string_ S\n", |
| 163 | + "17 unicode_ U" |
| 164 | + ] |
| 165 | + }, |
| 166 | + "execution_count": 2, |
| 167 | + "metadata": {}, |
| 168 | + "output_type": "execute_result" |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "import pandas as pd\n", |
| 173 | + "dtypes = pd.DataFrame(\n", |
| 174 | + " {\n", |
| 175 | + " 'Type': ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float16', 'float32', 'float64', 'float128', 'complex64', 'complex128', 'bool', 'object', 'string_', 'unicode_'],\n", |
| 176 | + " 'Type Code': ['i1', 'u1', 'i2', 'u2', 'i4', 'u4', 'i8', 'u8', 'f2', 'f4 or f', 'f8 or d', 'f16 or g', 'c8', 'c16', '', 'O', 'S', 'U']\n", |
| 177 | + " }\n", |
| 178 | + ")\n", |
| 179 | + "\n", |
| 180 | + "dtypes" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 3, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "name": "stdout", |
| 190 | + "output_type": "stream", |
| 191 | + "text": [ |
| 192 | + "[1. 2. 3.]\n", |
| 193 | + "float32\n", |
| 194 | + "[1.+2.j 3.-4.j]\n", |
| 195 | + "complex64\n", |
| 196 | + "[False True True]\n", |
| 197 | + "bool\n" |
| 198 | + ] |
| 199 | + } |
| 200 | + ], |
| 201 | + "source": [ |
| 202 | + "# create an array with a specified data type\n", |
| 203 | + "arr = np.array([1,2,3], dtype='f4')\n", |
| 204 | + "print(arr)\n", |
| 205 | + "print(arr.dtype)\n", |
| 206 | + "\n", |
| 207 | + "arr = np.array([1+2j, 3-4j], dtype=np.complex64)\n", |
| 208 | + "print(arr)\n", |
| 209 | + "print(arr.dtype)\n", |
| 210 | + "\n", |
| 211 | + "arr = np.array([0, 1, 1], dtype=np.bool)\n", |
| 212 | + "print(arr)\n", |
| 213 | + "print(arr.dtype)" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "markdown", |
| 218 | + "metadata": {}, |
| 219 | + "source": [ |
| 220 | + "### string data type" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 4, |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [ |
| 228 | + { |
| 229 | + "name": "stdout", |
| 230 | + "output_type": "stream", |
| 231 | + "text": [ |
| 232 | + "[b'abc' b'def']\n", |
| 233 | + "|S3\n" |
| 234 | + ] |
| 235 | + } |
| 236 | + ], |
| 237 | + "source": [ |
| 238 | + "# set the max length of the string using S + some number, such as 'S3'\n", |
| 239 | + "# any string longer than the max length will be truncated\n", |
| 240 | + "s = np.array(['abc', 'defg'], dtype='S3')\n", |
| 241 | + "print(s)\n", |
| 242 | + "print(s.dtype)" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": 5, |
| 248 | + "metadata": {}, |
| 249 | + "outputs": [ |
| 250 | + { |
| 251 | + "name": "stdout", |
| 252 | + "output_type": "stream", |
| 253 | + "text": [ |
| 254 | + "|S3\n", |
| 255 | + "<U3\n" |
| 256 | + ] |
| 257 | + } |
| 258 | + ], |
| 259 | + "source": [ |
| 260 | + "# numpy string and unicode data types are fixed-length\n", |
| 261 | + "# string_ and unicode_ will treat the longest string in the array as the default length when creating an array\n", |
| 262 | + "arr = np.array(['a', 'ab', 'abc'], dtype=np.string_)\n", |
| 263 | + "print(arr.dtype)\n", |
| 264 | + "\n", |
| 265 | + "arr = np.array(['a', 'ab', 'abc'], dtype=np.unicode_)\n", |
| 266 | + "print(arr.dtype)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": 6, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "# what does the above \"|\" and \"<\" mean?\n", |
| 276 | + "# they are the byte order indicators, which is beyond the scope of this tutorial\n", |
| 277 | + "# you can check it out here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.byteorder.html\n", |
| 278 | + "# and here: https://en.wikipedia.org/wiki/Endianness" |
| 279 | + ] |
| 280 | + } |
| 281 | + ], |
| 282 | + "metadata": { |
| 283 | + "kernelspec": { |
| 284 | + "display_name": "Python 3", |
| 285 | + "language": "python", |
| 286 | + "name": "python3" |
| 287 | + }, |
| 288 | + "language_info": { |
| 289 | + "codemirror_mode": { |
| 290 | + "name": "ipython", |
| 291 | + "version": 3 |
| 292 | + }, |
| 293 | + "file_extension": ".py", |
| 294 | + "mimetype": "text/x-python", |
| 295 | + "name": "python", |
| 296 | + "nbconvert_exporter": "python", |
| 297 | + "pygments_lexer": "ipython3", |
| 298 | + "version": "3.6.6" |
| 299 | + } |
| 300 | + }, |
| 301 | + "nbformat": 4, |
| 302 | + "nbformat_minor": 2 |
| 303 | +} |
0 commit comments