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

Method __setitem__

tables/table.py:3719–3774  ·  view source on GitHub ↗

Set a row or a range of rows in a column. If key argument is an integer, the corresponding element is set to value. If key is a slice, the range of elements determined by it is set to value. Examples -------- :: # Modify row 1

(self, key: int | slice, value: Any)

Source from the content-addressed store, hash-verified

3717 yield from buf_slice
3718
3719 def __setitem__(self, key: int | slice, value: Any) -> int:
3720 """Set a row or a range of rows in a column.
3721
3722 If key argument is an integer, the corresponding element is set to
3723 value. If key is a slice, the range of elements determined by it is
3724 set to value.
3725
3726 Examples
3727 --------
3728 ::
3729
3730 # Modify row 1
3731 table.cols.col1[1] = -1
3732
3733 # Modify rows 1 and 3
3734 table.cols.col1[1::2] = [2,3]
3735
3736 Which is equivalent to::
3737
3738 # Modify row 1
3739 table.modify_columns(start=1, columns=[[-1]], names=['col1'])
3740
3741 # Modify rows 1 and 3
3742 columns = np.rec.fromarrays([[2,3]], formats='i4')
3743 table.modify_columns(start=1, step=2, columns=columns,
3744 names=['col1'])
3745
3746 """
3747 table = self.table
3748 table._v_file._check_writable()
3749
3750 # Generalized key support not there yet, but at least allow
3751 # for a tuple with one single element (the main dimension).
3752 # (key,) --> key
3753 if isinstance(key, tuple) and len(key) == 1:
3754 key = key[0]
3755
3756 if is_idx(key):
3757 key = operator.index(key)
3758
3759 # Index out of range protection
3760 if key >= table.nrows:
3761 raise IndexError("Index out of range")
3762 if key < 0:
3763 # To support negative values
3764 key += table.nrows
3765 return table.modify_column(
3766 key, key + 1, 1, [[value]], self.pathname
3767 )
3768 elif isinstance(key, slice):
3769 start, stop, step = table._process_range(
3770 key.start, key.stop, key.step
3771 )
3772 return table.modify_column(start, stop, step, value, self.pathname)
3773 else:
3774 raise ValueError("Non-valid index or slice: %s" % key)
3775
3776 def create_index(

Callers

nothing calls this directly

Calls 5

is_idxFunction · 0.85
_check_writableMethod · 0.80
indexMethod · 0.80
modify_columnMethod · 0.80
_process_rangeMethod · 0.45

Tested by

no test coverage detected