named_array¶
Named arrays with typed, named columns backed by array.array and numpy.
This module provides :class:Field and :class:NamedArray for managing
columnar data with named fields, type formats, and efficient conversion to
numpy arrays and pandas DataFrames.
Example
narr = named_array.NamedArray('id=i,val=f')
narr.append(1, 10.0) # Append one row.
narr.data()['id'] # The per-field numpy arrays.
narr.to_numpy() # A 2D array - numeric, size==1 fields only.
Field
¶
Represents a single typed column within a NamedArray.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
The column name. |
|
data |
The underlying storage (array.array or list). |
|
size |
Number of elements per row (1 for scalar, >1 for fixed vectors). |
|
fmt |
The data format character code. |
|
str_tbl |
String table used for string columns ('S' format). |
Source code in py_misc_utils/named_array.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
np_array()
¶
Return the field's data as a numpy array.
Returns:
| Type | Description |
|---|---|
|
A numpy array of shape (n,) if size==1, or (n, size) otherwise. |
Source code in py_misc_utils/named_array.py
55 56 57 58 59 60 61 62 63 | |
append(arg)
¶
Append a single value to the field's data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
The value to append. For string fields ('S' format), it is interned via the string table. |
required |
Source code in py_misc_utils/named_array.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
extend(arg)
¶
Extend the field's data with multiple values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
An iterable of values to append. Its length must be a multiple of self.size. String fields are interned via the string table. |
required |
Source code in py_misc_utils/named_array.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
stringify(arg)
¶
Intern string values through the string table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
A string or tuple of strings to intern. |
required |
Returns:
| Type | Description |
|---|---|
|
The interned string (or tuple of interned strings). |
Source code in py_misc_utils/named_array.py
100 101 102 103 104 105 106 107 108 109 110 111 112 | |
create(name, fmt, str_tbl)
staticmethod
¶
Create a Field from a name and a format specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
The column name. |
required | |
fmt
|
Format specification, optionally holding a size prefix (e.g. '3f' = 3 float32 per row); non-numeric formats ('S', 'x') fall back to a plain list storage. |
required | |
str_tbl
|
String table used for string columns. |
required |
Returns:
| Type | Description |
|---|---|
|
The created Field. |
Source code in py_misc_utils/named_array.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
NamedArray
¶
A columnar data container with named, typed fields.
Fields are defined by name=format pairs - comma-separated, e.g. 'id=i,name=S' - where the format is an array.array type code, optionally prefixed with a fixed vector size (e.g. '3f' for a 3-elements float vector per row). String fields use the 'S' code, whose values are interned in a per-array string table.
Supports appending rows, extending from other NamedArrays, conversion to numpy arrays and pandas DataFrames.
Example
narr = named_array.NamedArray('id=i,val=f,name=S')
narr.append(1, 10.0, 'alice')
narr.kwappend(id=2, val=20.0, name='bob')
narr.shape # (2, 3) - rows and fields.
narr[0] # (1, 10.0, 'alice') - a whole row as a tuple.
Source code in py_misc_utils/named_array.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
shape
property
¶
Return the (rows, columns) shape of the array.
Returns:
| Type | Description |
|---|---|
|
A tuple with the number of rows and the number of fields. |
add_column(name, fmt, data)
¶
Add a new column to the NamedArray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
The column name (must not already exist). |
required | |
fmt
|
The format character code for the column. |
required | |
data
|
The column data (list, array, or numpy array). |
required |
Source code in py_misc_utils/named_array.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
append(*args)
¶
Append a row of values to each field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
One value per field, in field order. Vector fields take a sequence of self.size values. |
()
|
Example
narr = named_array.NamedArray('id=i,name=S')
narr.append(1, 'alice') # One full row.
Source code in py_misc_utils/named_array.py
248 249 250 251 252 253 254 255 256 257 258 259 260 | |
extend(other)
¶
Extend this NamedArray with rows from another NamedArray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another NamedArray with the same fields. |
required |
Source code in py_misc_utils/named_array.py
262 263 264 265 266 267 268 269 | |
append_extend(*args)
¶
Extend each field with iterables of values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
One iterable per field, in field order. |
()
|
Source code in py_misc_utils/named_array.py
271 272 273 274 275 276 277 278 | |
kwappend(**kwargs)
¶
Append a row using keyword arguments keyed by field name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Field name to value mappings. |
{}
|
Source code in py_misc_utils/named_array.py
280 281 282 283 284 285 286 287 288 | |
get_tuple_item(i)
¶
Return the i-th row as a tuple of field values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
The row index. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of values, one per field. Alias of getitem. |
Source code in py_misc_utils/named_array.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
get_arrays(names=None)
¶
Return numpy arrays for the specified fields, or all fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
Optional collection of field names. If None, all fields are returned. |
None
|
Returns:
| Type | Description |
|---|---|
|
A tuple of numpy arrays, one per requested field - shaped (n,) for |
|
|
scalar fields and (n, size) for vector fields. |
Source code in py_misc_utils/named_array.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
get_array(name)
¶
Return the numpy array for a single field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
The field name. |
required |
Returns:
| Type | Description |
|---|---|
|
A numpy array containing the field's data. |
Source code in py_misc_utils/named_array.py
320 321 322 323 324 325 326 327 328 329 | |
data()
¶
Return a mapping of field name to its numpy array data.
Returns:
| Type | Description |
|---|---|
|
A dict mapping field names to their numpy arrays. |
Source code in py_misc_utils/named_array.py
331 332 333 334 335 336 337 | |
to_numpy(dtype=None)
¶
Convert all fields to a single 2D numpy array.
All fields must be numeric with size==1 - string ('S') and vector fields cannot be converted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
Optional numpy dtype. If None, inferred from field dtypes. |
None
|
Returns:
| Type | Description |
|---|---|
|
A numpy array of shape (n_rows, n_fields). |
Source code in py_misc_utils/named_array.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
to_dataframe()
¶
Convert the NamedArray into a pandas DataFrame.
Returns:
| Type | Description |
|---|---|
|
A DataFrame with one column per field, holding the numpy arrays of |
|
|
the scalar fields and the lists of the vector fields. |
Example
narr = named_array.NamedArray('id=i,name=S')
narr.append(1, 'alice')
narr.to_dataframe().to_dict('list') # {'id': [1], 'name': ['alice']}
Source code in py_misc_utils/named_array.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
dtypes()
¶
Return the numpy dtypes of the fields.
Returns:
| Type | Description |
|---|---|
|
A tuple with one numpy dtype per field: non-numeric fields get |
|
|
dtype('O'). |
Source code in py_misc_utils/named_array.py
439 440 441 442 443 444 445 446 447 448 449 450 | |