Skip to content

Commit

Permalink
Wrap the Table slice op (#3110)
Browse files Browse the repository at this point in the history
* Wrap the Table slice op

* Respond to review comments
  • Loading branch information
jmao-denver authored Nov 30, 2022
1 parent fe4eeaa commit 368a6d5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
32 changes: 32 additions & 0 deletions py/server/deephaven/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,38 @@ def update_by(self, ops: Union[UpdateByOperation, List[UpdateByOperation]],
except Exception as e:
raise DHError(e, "table update-by operation failed.") from e

def slice(self, start: int, stop: int) -> Table:
"""Extracts a subset of a table by row positions into a new Table.
If both the start and the stop are positive, then both are counted from the beginning of the table.
The start is inclusive, and the stop is exclusive. slice(0, N) is equivalent to :meth:`~Table.head(N)`
The start must be less than or equal to the stop.
If the start is positive and the stop is negative, then the start is counted from the beginning of the
table, inclusively. The stop is counted from the end of the table. For example, slice(1, -1) includes all
rows but the first and last. If the stop is before the start, the result is an empty table.
If the start is negative, and the stop is zero, then the start is counted from the end of the table,
and the end of the slice is the size of the table. slice(-N, 0) is equivalent to :meth:`~Table.tail(N)`.
If the start is negative and the stop is negative, they are both counted from the end of the
table. For example, slice(-2, -1) returns the second to last row of the table.
Args:
start (int): the first row position to include in the result
stop (int): the last row position to include in the result
Returns:
a new Table
Raises:
DHError
"""
try:
return Table(j_table=self.j_table.slice(start, stop))
except Exception as e:
raise DHError(e, "table slice operation failed.") from e


class PartitionedTable(JObjectWrapper):
"""A partitioned table is a table containing tables, known as constituent tables.
Expand Down
27 changes: 27 additions & 0 deletions py/server/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,33 @@ def test_python_field_access(self):
self.assertIn("AAPL-GOOG", html_output)
self.assertIn("2000", html_output)

def test_slice(self):
with ugp.shared_lock():
t = time_table("00:00:00.01")
rt = t.slice(0, 3)
self.assert_table_equals(t.head(3), rt)

self.wait_ticking_table_update(t, row_count=5, timeout=5)
with ugp.shared_lock():
rt = t.slice(t.size, -2)
self.assertEqual(0, rt.size)
self.wait_ticking_table_update(rt, row_count=1, timeout=5)
self.assertGreaterEqual(rt.size, 1)

rt = t.slice(-3, 0)
self.assert_table_equals(t.tail(3), rt)

rt = t.slice(-3, -2)
self.wait_ticking_table_update(rt, row_count=1, timeout=5)
self.assert_table_equals(t.tail(3).head(1), rt)

rt = t.slice(1, 3)
self.wait_ticking_table_update(rt, row_count=2, timeout=5)
self.assert_table_equals(t.head(3).tail(2), rt)

with self.assertRaises(DHError):
rt = t.slice(3, 2)


def global_fn() -> str:
return "global str"
Expand Down

0 comments on commit 368a6d5

Please sign in to comment.