Modify a series of columns in the row slice [start:stop:step]. The names argument specifies the names of the columns in the table to be modified with the data given in columns. This method returns the number of rows modified. Should the modification exceed the leng
(
self,
start: int | None = None,
stop: int | None = None,
step: int | None = None,
columns: Sequence | None = None,
names: list[str] | tuple[str, ...] | None = None,
)
| 2623 | return SizeType(nrows) |
| 2624 | |
| 2625 | def modify_columns( |
| 2626 | self, |
| 2627 | start: int | None = None, |
| 2628 | stop: int | None = None, |
| 2629 | step: int | None = None, |
| 2630 | columns: Sequence | None = None, |
| 2631 | names: list[str] | tuple[str, ...] | None = None, |
| 2632 | ) -> int: |
| 2633 | """Modify a series of columns in the row slice [start:stop:step]. |
| 2634 | |
| 2635 | The names argument specifies the names of the columns in the |
| 2636 | table to be modified with the data given in columns. This |
| 2637 | method returns the number of rows modified. Should the |
| 2638 | modification exceed the length of the table, an IndexError |
| 2639 | is raised before changing data. |
| 2640 | |
| 2641 | The columns argument may be any object which can be converted |
| 2642 | to a structured array compliant with the structure of the |
| 2643 | columns to be modified (otherwise, a ValueError is raised). |
| 2644 | This includes NumPy structured arrays, lists of tuples or array |
| 2645 | records, and a string or Python buffer. |
| 2646 | |
| 2647 | """ |
| 2648 | if step is None: |
| 2649 | step = 1 |
| 2650 | if type(names) not in (list, tuple): |
| 2651 | raise TypeError("The 'names' parameter must be a list of strings.") |
| 2652 | |
| 2653 | if columns is None: # Nothing to be done |
| 2654 | return SizeType(0) |
| 2655 | if start is None: |
| 2656 | start = 0 |
| 2657 | if start < 0: |
| 2658 | raise ValueError("'start' must have a positive value.") |
| 2659 | if step < 1: |
| 2660 | raise ValueError( |
| 2661 | "'step' must have a value greater or equal than 1." |
| 2662 | ) |
| 2663 | descr = [] |
| 2664 | for colname in names: |
| 2665 | objcol = self._get_column_instance(colname) |
| 2666 | descr.append(objcol._v_parent._v_nested_descr[objcol._v_pos]) |
| 2667 | # descr.append(objcol._v_parent._v_dtype[objcol._v_pos]) |
| 2668 | # Try to convert the columns object into a recarray |
| 2669 | try: |
| 2670 | # Make sure the result is always a *copy* of the original, |
| 2671 | # so the resulting object is safe for in-place conversion. |
| 2672 | iflavor = flavor_of(columns) |
| 2673 | if iflavor != "python": |
| 2674 | columns = array_as_internal(columns, iflavor) |
| 2675 | recarray = np.rec.array(columns, dtype=descr) |
| 2676 | else: |
| 2677 | recarray = np.rec.fromarrays(columns, dtype=descr) |
| 2678 | except Exception as exc: # XXX |
| 2679 | raise ValueError( |
| 2680 | f"columns parameter cannot be converted into a " |
| 2681 | f"recarray object compliant with table '{self}'. " |
| 2682 | f"The error was: <{exc}>" |