Append a sequence of rows to the end of the table. The rows argument may be any object which can be converted to a structured array compliant with the table structure (otherwise, a ValueError is raised). This includes NumPy structured arrays, lists of tuples or arra
(self, rows: list | np.ndarray)
| 2331 | self._mark_columns_as_dirty(self.colpathnames) |
| 2332 | |
| 2333 | def append(self, rows: list | np.ndarray) -> None: |
| 2334 | """Append a sequence of rows to the end of the table. |
| 2335 | |
| 2336 | The rows argument may be any object which can be converted to |
| 2337 | a structured array compliant with the table structure |
| 2338 | (otherwise, a ValueError is raised). This includes NumPy |
| 2339 | structured arrays, lists of tuples or array records, and a |
| 2340 | string or Python buffer. |
| 2341 | |
| 2342 | Examples |
| 2343 | -------- |
| 2344 | :: |
| 2345 | |
| 2346 | import tables as tb |
| 2347 | |
| 2348 | class Particle(tb.IsDescription): |
| 2349 | name = tb.StringCol(16, pos=1) # 16-character String |
| 2350 | lati = tb.IntCol(pos=2) # integer |
| 2351 | longi = tb.IntCol(pos=3) # integer |
| 2352 | pressure = tb.Float32Col(pos=4) # float (single-precision) |
| 2353 | temperature = tb.FloatCol(pos=5) # double (double-precision) |
| 2354 | |
| 2355 | fileh = tb.open_file('test4.h5', mode='w') |
| 2356 | table = fileh.create_table(fileh.root, 'table', Particle, |
| 2357 | "A table") |
| 2358 | |
| 2359 | # Append several rows in only one call |
| 2360 | table.append([("Particle: 10", 10, 0, 10 * 10, 10**2), |
| 2361 | ("Particle: 11", 11, -1, 11 * 11, 11**2), |
| 2362 | ("Particle: 12", 12, -2, 12 * 12, 12**2)]) |
| 2363 | fileh.close() |
| 2364 | |
| 2365 | """ |
| 2366 | self._g_check_open() |
| 2367 | self._v_file._check_writable() |
| 2368 | |
| 2369 | if not self._chunked: |
| 2370 | raise HDF5ExtError( |
| 2371 | "You cannot append rows to a non-chunked table.", h5bt=False |
| 2372 | ) |
| 2373 | |
| 2374 | if ( |
| 2375 | hasattr(rows, "dtype") |
| 2376 | and not self.description._v_is_nested |
| 2377 | and rows.dtype == self.dtype |
| 2378 | ): |
| 2379 | # Shortcut for compliant arrays |
| 2380 | # (for some reason, not valid for nested types) |
| 2381 | wbuf_ra = rows |
| 2382 | else: |
| 2383 | # Try to convert the object into a recarray compliant with table |
| 2384 | try: |
| 2385 | iflavor = flavor_of(rows) |
| 2386 | if iflavor != "python": |
| 2387 | rows = array_as_internal(rows, iflavor) |
| 2388 | # Works for Python structures and always copies the original, |
| 2389 | # so the resulting object is safe for in-place conversion. |
| 2390 | wbuf_ra = np.rec.array(rows, dtype=self._v_dtype) |