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

Method __getitem__

tables/table.py:3636–3702  ·  view source on GitHub ↗

Get a row or a range of rows from a column. If key argument is an integer, the corresponding element in the column is returned as an object of the current flavor. If key is a slice, the range of elements determined by it is returned as an array of the current flavor

(self, key: int | slice)

Source from the content-addressed store, hash-verified

3634 return self.table.nrows
3635
3636 def __getitem__(self, key: int | slice) -> np.ndarray:
3637 """Get a row or a range of rows from a column.
3638
3639 If key argument is an integer, the corresponding element in the column
3640 is returned as an object of the current flavor. If key is a slice, the
3641 range of elements determined by it is returned as an array of the
3642 current flavor.
3643
3644 Examples
3645 --------
3646 ::
3647
3648 print("Column handlers:")
3649 for name in table.colnames:
3650 print(table.cols._f_col(name))
3651 print("Select table.cols.name[1]-->", table.cols.name[1])
3652 print("Select table.cols.name[1:2]-->", table.cols.name[1:2])
3653 print("Select table.cols.name[:]-->", table.cols.name[:])
3654 print("Select table.cols._f_col('name')[:]-->",
3655 table.cols._f_col('name')[:])
3656
3657 The output of this for a certain arbitrary table is::
3658
3659 Column handlers:
3660 /table.cols.name (Column(), string, idx=None)
3661 /table.cols.lati (Column(), int32, idx=None)
3662 /table.cols.longi (Column(), int32, idx=None)
3663 /table.cols.vector (Column(2,), int32, idx=None)
3664 /table.cols.matrix2D (Column(2, 2), float64, idx=None)
3665 Select table.cols.name[1]--> Particle: 11
3666 Select table.cols.name[1:2]--> ['Particle: 11']
3667 Select table.cols.name[:]--> ['Particle: 10'
3668 'Particle: 11' 'Particle: 12'
3669 'Particle: 13' 'Particle: 14']
3670 Select table.cols._f_col('name')[:]--> ['Particle: 10'
3671 'Particle: 11' 'Particle: 12'
3672 'Particle: 13' 'Particle: 14']
3673
3674 See the :file:`examples/table2.py` file for a more complete example.
3675
3676 """
3677 table = self.table
3678
3679 # Generalized key support not there yet, but at least allow
3680 # for a tuple with one single element (the main dimension).
3681 # (key,) --> key
3682 if isinstance(key, tuple) and len(key) == 1:
3683 key = key[0]
3684
3685 if is_idx(key):
3686 key = operator.index(key)
3687
3688 # Index out of range protection
3689 if key >= table.nrows:
3690 raise IndexError("Index out of range")
3691 if key < 0:
3692 # To support negative values
3693 key += table.nrows

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected