MCPcopy Index your code
hub / github.com/pathwaycom/pathway / diff

Function diff

python/pathway/stdlib/ordered/diff.py:10–123  ·  view source on GitHub ↗

Compute the difference between the values in the ``values`` columns and the previous values according to the order defined by the column ``timestamp``. Args: timestamp (pw.ColumnReference[int | float | datetime | str | bytes]): The column reference to the ``timesta

(
    self: pw.Table,
    timestamp: pw.ColumnReference,
    *values: pw.ColumnReference,
    instance: pw.ColumnReference | None = None,
)

Source from the content-addressed store, hash-verified

8@check_arg_types
9@trace_user_frame
10def diff(
11 self: pw.Table,
12 timestamp: pw.ColumnReference,
13 *values: pw.ColumnReference,
14 instance: pw.ColumnReference | None = None,
15) -> pw.Table:
16 """
17 Compute the difference between the values in the ``values`` columns and the previous values
18 according to the order defined by the column ``timestamp``.
19
20 Args:
21
22 timestamp (pw.ColumnReference[int | float | datetime | str | bytes]):
23 The column reference to the ``timestamp`` column on which the order is computed.
24 *values (pw.ColumnReference[int | float | datetime]):
25 Variable-length argument representing the column references to the ``values`` columns.
26 instance (pw.ColumnReference):
27 Can be used to group the values. The difference is only computed between rows with
28 the same ``instance`` value.
29
30 Returns:
31 ``Table``: A new table where each column is replaced with a new column containing
32 the difference and whose name is the concatenation of `diff_` and the former name.
33
34 Raises:
35 ValueError: If the columns are not ColumnReference.
36
37 Note:
38 - The value of the "first" value (the row with the lowest value \
39 in the ``timestamp`` column) is ``None``.
40
41 Example:
42
43 >>> import pathway as pw
44 >>> table = pw.debug.table_from_markdown('''
45 ... timestamp | values
46 ... 1 | 1
47 ... 2 | 2
48 ... 3 | 4
49 ... 4 | 7
50 ... 5 | 11
51 ... 6 | 16
52 ... ''')
53 >>> table += table.diff(pw.this.timestamp, pw.this.values)
54 >>> pw.debug.compute_and_print(table, include_id=False)
55 timestamp | values | diff_values
56 1 | 1 |
57 2 | 2 | 1
58 3 | 4 | 2
59 4 | 7 | 3
60 5 | 11 | 4
61 6 | 16 | 5
62
63 >>> table = pw.debug.table_from_markdown(
64 ... '''
65 ... timestamp | instance | values
66 ... 1 | 0 | 1
67 ... 2 | 1 | 2

Callers

nothing calls this directly

Calls 5

sortMethod · 0.80
selectMethod · 0.45
ixMethod · 0.45
renameMethod · 0.45
withoutMethod · 0.45

Tested by

no test coverage detected