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

Method __setitem__

tables/table.py:2242–2315  ·  view source on GitHub ↗

Set a row or a range of rows in the table. It takes different actions depending on the type of the *key* parameter: if it is an integer, the corresponding table row is set to *value* (a record or sequence capable of being converted to the table structure). If *key*

(
        self,
        key: int | slice | list[int] | list[bool] | np.ndarray,
        value: Any,
    )

Source from the content-addressed store, hash-verified

2240 raise IndexError(f"Invalid index or slice: {key!r}")
2241
2242 def __setitem__(
2243 self,
2244 key: int | slice | list[int] | list[bool] | np.ndarray,
2245 value: Any,
2246 ) -> int:
2247 """Set a row or a range of rows in the table.
2248
2249 It takes different actions depending on the type of the *key*
2250 parameter: if it is an integer, the corresponding table row is
2251 set to *value* (a record or sequence capable of being converted
2252 to the table structure). If *key* is a slice, the row slice
2253 determined by it is set to *value* (a record array or sequence
2254 capable of being converted to the table structure).
2255
2256 In addition, NumPy-style point selections are supported. In
2257 particular, if key is a list of row coordinates, the set of rows
2258 determined by it is set to value. Furthermore, if key is an array of
2259 boolean values, only the coordinates where key is True are set to
2260 values from value. Note that for the latter to work it is necessary
2261 that key list would contain exactly as many rows as the table has.
2262
2263 Examples
2264 --------
2265 ::
2266
2267 # Modify just one existing row
2268 table[2] = [456,'db2',1.2]
2269
2270 # Modify two existing rows
2271 rows = np.rec.array(
2272 [[457,'db1',1.2],[6,'de2',1.3]], formats='i4,S3,f8'
2273 )
2274 table[1:30:2] = rows # modify a table slice
2275 table[[1,3]] = rows # only modifies rows 1 and 3
2276 table[[True,False,True]] = rows # only modifies rows 0 and 2
2277
2278 Which is equivalent to::
2279
2280 table.modify_rows(start=2, rows=[456,'db2',1.2])
2281 rows = np.rec.array(
2282 [[457,'db1',1.2],[6,'de2',1.3]], formats='i4,S3,f8'
2283 )
2284 table.modify_rows(start=1, stop=3, step=2, rows=rows)
2285 table.modify_coordinates([1,3,2], rows)
2286 table.modify_coordinates([True, False, True], rows)
2287
2288 Here, you can see how indexing can be used as a shorthand for the
2289 :meth:`Table.modify_rows` and :meth:`Table.modify_coordinates`
2290 methods.
2291
2292 """
2293 self._g_check_open()
2294 self._v_file._check_writable()
2295
2296 if is_idx(key):
2297 key = operator.index(key)
2298
2299 # Index out of range protection

Callers

nothing calls this directly

Calls 7

modify_rowsMethod · 0.95
modify_coordinatesMethod · 0.95
is_idxFunction · 0.85
_g_check_openMethod · 0.80
_check_writableMethod · 0.80
indexMethod · 0.80
_process_rangeMethod · 0.45

Tested by

no test coverage detected