r"""Iterate over values fulfilling a condition. This method returns a Row iterator (see :ref:`RowClassDescr`) which only selects rows in the table that satisfy the given condition (an expression-like string). The condvars mapping may be used to define the variable n
(
self,
condition: str,
condvars: dict[str, Column | np.ndarray] | None = None,
start: str | None = None,
stop: str | None = None,
step: str | None = None,
)
| 1477 | return frozenset(idxcols) |
| 1478 | |
| 1479 | def where( |
| 1480 | self, |
| 1481 | condition: str, |
| 1482 | condvars: dict[str, Column | np.ndarray] | None = None, |
| 1483 | start: str | None = None, |
| 1484 | stop: str | None = None, |
| 1485 | step: str | None = None, |
| 1486 | ) -> Iterator[tableextension.Row]: |
| 1487 | r"""Iterate over values fulfilling a condition. |
| 1488 | |
| 1489 | This method returns a Row iterator (see :ref:`RowClassDescr`) which |
| 1490 | only selects rows in the table that satisfy the given condition (an |
| 1491 | expression-like string). |
| 1492 | |
| 1493 | The condvars mapping may be used to define the variable names appearing |
| 1494 | in the condition. condvars should consist of identifier-like strings |
| 1495 | pointing to Column (see :ref:`ColumnClassDescr`) instances *of this |
| 1496 | table*, or to other values (which will be converted to arrays). A |
| 1497 | default set of condition variables is provided where each top-level, |
| 1498 | non-nested column with an identifier-like name appears. Variables in |
| 1499 | condvars override the default ones. |
| 1500 | |
| 1501 | When condvars is not provided or None, the current local and global |
| 1502 | namespace is sought instead of condvars. The previous mechanism is |
| 1503 | mostly intended for interactive usage. To disable it, just specify a |
| 1504 | (maybe empty) mapping as condvars. |
| 1505 | |
| 1506 | If a range is supplied (by setting some of the start, stop or step |
| 1507 | parameters), only the rows in that range and fulfilling the condition |
| 1508 | are used. The meaning of the start, stop and step parameters is the |
| 1509 | same as for Python slices. |
| 1510 | |
| 1511 | When possible, indexed columns participating in the condition will be |
| 1512 | used to speed up the search. It is recommended that you place the |
| 1513 | indexed columns as left and out in the condition as possible. Anyway, |
| 1514 | this method has always better performance than regular Python |
| 1515 | selections on the table. |
| 1516 | |
| 1517 | You can mix this method with regular Python selections in order to |
| 1518 | support even more complex queries. It is strongly recommended that you |
| 1519 | pass the most restrictive condition as the parameter to this method if |
| 1520 | you want to achieve maximum performance. |
| 1521 | |
| 1522 | .. warning:: |
| 1523 | |
| 1524 | When in the middle of a table row iterator, you should not |
| 1525 | use methods that can change the number of rows in the table |
| 1526 | (like :meth:`Table.append` or :meth:`Table.remove_rows`) or |
| 1527 | unexpected errors will happen. |
| 1528 | |
| 1529 | Examples |
| 1530 | -------- |
| 1531 | :: |
| 1532 | |
| 1533 | passvalues = [ row['col3'] for row in |
| 1534 | table.where('(col1 > 0) & (col2 <= 20)', step=5) |
| 1535 | if your_function(row['col2']) ] |
| 1536 | print("Values that pass the cuts:", passvalues) |