slice
The slice
method returns a table that is a subset of another table based on row positions.
Attempting to use slice
on a blink table will raise an error.
Syntax
table.slice(start: int, stop: int) -> Table
Parameters
Parameter | Type | Description |
---|---|---|
start | int | The row index at which the slice starts. If positive, the row index is counted down from the top of the table. If negative, the row index is counted up from the bottom of the table. This index is inclusive. |
stop | int | The row index at which the slice ends. If positive, the row index is counted down from the top of the table. If negative, the row index is counted up from the bottom of the table. This index is not inclusive. |
Returns
A new table with a number of rows corresponding to the difference between the start and end row indices. If the start index is negative, the end index must also be less than or equal to 0, and greater than or equal to the start index. If these conditions are not met, an error will occur.
Example
The following example filters the table to the middle 50 rows.
from deephaven import empty_table
source = empty_table(100).update(["X = i"])
result = source.slice(25, 75)
- source
- result
The following example uses negative start and end indices to filter the table to those from the fifth-to-last to the second-to-last.
from deephaven import empty_table
source = empty_table(100).update(["X = i"])
result = source.slice(-5, -1)
- source
- result
The following example uses a positive start index and negative end index to filter out rows from the start and end of the table.
from deephaven import empty_table
source = empty_table(100).update(["X = i"])
result = source.slice(10, -10)
- source
- result
The following example uses a negative start index and positive end index to return all rows starting from the third-last row to the fifth row of the table.
from deephaven import empty_table
source = empty_table(5).update(["X = i"])
result = source.slice(-3, 5)
- source
- result
The following example shows how the use of row index 0 as the start or end index filters a table to its first or last N rows.
from deephaven import empty_table
source = empty_table(100).update(["X = i"])
slice_head = source.slice(0, 20)
head = source.head(20)
slice_tail = source.slice(-20, 0)
tail = source.tail(20)
- source
- slice_head
- head
- slice_tail
- tail