MCPcopy Create free account
hub / github.com/PyTables/PyTables / __getitem__

Method __getitem__

tables/table.py:3357–3413  ·  view source on GitHub ↗

Get a row or a range of rows from a table or nested column. If key argument is an integer, the corresponding nested type row is returned as a record of the current flavor. If key is a slice, the range of rows determined by it is returned as a structured array of the

(self, key: int | slice)

Source from the content-addressed store, hash-verified

3355 return cols
3356
3357 def __getitem__(self, key: int | slice) -> Any:
3358 """Get a row or a range of rows from a table or nested column.
3359
3360 If key argument is an integer, the corresponding nested type row is
3361 returned as a record of the current flavor. If key is a slice, the
3362 range of rows determined by it is returned as a structured array of the
3363 current flavor.
3364
3365 Examples
3366 --------
3367 ::
3368
3369 record = table.cols[4] # equivalent to table[4]
3370 recarray = table.cols.Info[4:1000:2]
3371
3372 Those statements are equivalent to::
3373
3374 nrecord = table.read(start=4)[0]
3375 nrecarray = table.read(start=4, stop=1000, step=2).field('Info')
3376
3377 Here you can see how a mix of natural naming, indexing and slicing can
3378 be used as shorthands for the :meth:`Table.read` method.
3379
3380 """
3381 table = self._v_table
3382 nrows = table.nrows
3383 if is_idx(key):
3384 key = operator.index(key)
3385
3386 # Index out of range protection
3387 if key >= nrows:
3388 raise IndexError("Index out of range")
3389 if key < 0:
3390 # To support negative values
3391 key += nrows
3392 start, stop, step = table._process_range(key, key + 1, 1)
3393 colgroup = self._v_desc._v_pathname
3394 if colgroup == "": # The root group
3395 return table.read(start, stop, step)[0]
3396 else:
3397 crecord = table.read(start, stop, step)[0]
3398 return crecord[colgroup]
3399 elif isinstance(key, slice):
3400 start, stop, step = table._process_range(
3401 key.start, key.stop, key.step
3402 )
3403 colgroup = self._v_desc._v_pathname
3404 if colgroup == "": # The root group
3405 return table.read(start, stop, step)
3406 else:
3407 crecarray = table.read(start, stop, step)
3408 if hasattr(crecarray, "field"):
3409 return crecarray.field(colgroup) # RecArray case
3410 else:
3411 return get_nested_field(crecarray, colgroup) # numpy case
3412 else:
3413 raise TypeError(f"invalid index or slice: {key!r}")
3414

Callers 6

_g_copy_with_statsMethod · 0.45
_g_copy_with_statsMethod · 0.45
_guess_shapeMethod · 0.45
evalMethod · 0.45
__iter__Method · 0.45
_computeFunction · 0.45

Calls 4

is_idxFunction · 0.85
indexMethod · 0.80
_process_rangeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected