Modify one single column in the row slice [start:stop:step]. The colname argument specifies the name of the column in the table to be modified with the data given in column. This method returns the number of rows modified. Should the modification exceed the length
(
self,
start: int | None = None,
stop: int | None = None,
step: int | None = None,
column: Sequence | None = None,
colname: str | None = None,
)
| 2529 | return SizeType(lenrows) |
| 2530 | |
| 2531 | def modify_column( |
| 2532 | self, |
| 2533 | start: int | None = None, |
| 2534 | stop: int | None = None, |
| 2535 | step: int | None = None, |
| 2536 | column: Sequence | None = None, |
| 2537 | colname: str | None = None, |
| 2538 | ): |
| 2539 | """Modify one single column in the row slice [start:stop:step]. |
| 2540 | |
| 2541 | The colname argument specifies the name of the column in the |
| 2542 | table to be modified with the data given in column. This |
| 2543 | method returns the number of rows modified. Should the |
| 2544 | modification exceed the length of the table, an IndexError is |
| 2545 | raised before changing data. |
| 2546 | |
| 2547 | The *column* argument may be any object which can be converted |
| 2548 | to a (record) array compliant with the structure of the column |
| 2549 | to be modified (otherwise, a ValueError is raised). This |
| 2550 | includes NumPy (record) arrays, lists of scalars, tuples or |
| 2551 | array records, and a string or Python buffer. |
| 2552 | |
| 2553 | """ |
| 2554 | if step is None: |
| 2555 | step = 1 |
| 2556 | if not isinstance(colname, str): |
| 2557 | raise TypeError("The 'colname' parameter must be a string.") |
| 2558 | self._v_file._check_writable() |
| 2559 | |
| 2560 | if column is None: # Nothing to be done |
| 2561 | return SizeType(0) |
| 2562 | if start is None: |
| 2563 | start = 0 |
| 2564 | |
| 2565 | if start < 0: |
| 2566 | raise ValueError("'start' must have a positive value.") |
| 2567 | if step < 1: |
| 2568 | raise ValueError( |
| 2569 | "'step' must have a value greater or equal than 1." |
| 2570 | ) |
| 2571 | # Get the column format to be modified: |
| 2572 | objcol = self._get_column_instance(colname) |
| 2573 | descr = [objcol._v_parent._v_nested_descr[objcol._v_pos]] |
| 2574 | # Try to convert the column object into a NumPy ndarray |
| 2575 | try: |
| 2576 | # If the column is a recarray (or kind of), convert into ndarray |
| 2577 | if hasattr(column, "dtype") and column.dtype.kind == "V": |
| 2578 | column = np.rec.array(column, dtype=descr).field(0) |
| 2579 | else: |
| 2580 | # Make sure the result is always a *copy* of the original, |
| 2581 | # so the resulting object is safe for in-place conversion. |
| 2582 | iflavor = flavor_of(column) |
| 2583 | column = array_as_internal(column, iflavor) |
| 2584 | except Exception as exc: # XXX |
| 2585 | raise ValueError( |
| 2586 | f"column parameter cannot be converted into a " |
| 2587 | f"ndarray object compliant with specified column " |
| 2588 | f"'{column}'. The error was: <{exc}>" |