@@ -52,8 +52,8 @@ def unbound(
52
52
### Rows-based Windows
53
53
def rows (
54
54
grouping_keys : Tuple [str , ...] = (),
55
- preceding : Optional [int ] = None ,
56
- following : Optional [int ] = None ,
55
+ start : Optional [int ] = None ,
56
+ end : Optional [int ] = None ,
57
57
min_periods : int = 0 ,
58
58
ordering : Tuple [orderings .OrderingExpression , ...] = (),
59
59
) -> WindowSpec :
@@ -63,18 +63,23 @@ def rows(
63
63
Args:
64
64
grouping_keys:
65
65
Columns ids of grouping keys
66
- preceding:
67
- number of preceding rows to include. If None, include all preceding rows
66
+ start:
67
+ The window's starting boundary relative to the current row. For example, "-1" means one row prior
68
+ "1" means one row after, and "0" means the current row. If None, the window is unbounded from the start.
68
69
following:
69
- number of following rows to include. If None, include all following rows
70
+ The window's ending boundary relative to the current row. For example, "-1" means one row prior
71
+ "1" means one row after, and "0" means the current row. If None, the window is unbounded until the end.
70
72
min_periods (int, default 0):
71
73
Minimum number of input rows to generate output.
72
74
ordering:
73
75
Ordering to apply on top of based dataframe ordering
74
76
Returns:
75
77
WindowSpec
76
78
"""
77
- bounds = RowsWindowBounds (preceding = preceding , following = following )
79
+ bounds = RowsWindowBounds (
80
+ start = start ,
81
+ end = end ,
82
+ )
78
83
return WindowSpec (
79
84
grouping_keys = tuple (map (ex .deref , grouping_keys )),
80
85
bounds = bounds ,
@@ -97,7 +102,7 @@ def cumulative_rows(
97
102
Returns:
98
103
WindowSpec
99
104
"""
100
- bounds = RowsWindowBounds (following = 0 )
105
+ bounds = RowsWindowBounds (end = 0 )
101
106
return WindowSpec (
102
107
grouping_keys = tuple (map (ex .deref , grouping_keys )),
103
108
bounds = bounds ,
@@ -119,7 +124,7 @@ def inverse_cumulative_rows(
119
124
Returns:
120
125
WindowSpec
121
126
"""
122
- bounds = RowsWindowBounds (preceding = 0 )
127
+ bounds = RowsWindowBounds (start = 0 )
123
128
return WindowSpec (
124
129
grouping_keys = tuple (map (ex .deref , grouping_keys )),
125
130
bounds = bounds ,
@@ -132,18 +137,35 @@ def inverse_cumulative_rows(
132
137
133
138
@dataclass (frozen = True )
134
139
class RowsWindowBounds :
135
- preceding : Optional [int ] = None
136
- following : Optional [int ] = None
137
-
140
+ start : Optional [int ] = None
141
+ end : Optional [int ] = None
138
142
139
- # TODO: Expand to datetime offsets
140
- OffsetType = Union [float , int ]
143
+ def __post_init__ (self ):
144
+ if self .start is None :
145
+ return
146
+ if self .end is None :
147
+ return
148
+ if self .start > self .end :
149
+ raise ValueError (
150
+ f"Invalid window: start({ self .start } ) is greater than end({ self .end } )"
151
+ )
141
152
142
153
143
154
@dataclass (frozen = True )
144
155
class RangeWindowBounds :
145
- preceding : Optional [OffsetType ] = None
146
- following : Optional [OffsetType ] = None
156
+ # TODO(b/388916840) Support range rolling on timeseries with timedeltas.
157
+ start : Optional [int ] = None
158
+ end : Optional [int ] = None
159
+
160
+ def __post_init__ (self ):
161
+ if self .start is None :
162
+ return
163
+ if self .end is None :
164
+ return
165
+ if self .start > self .end :
166
+ raise ValueError (
167
+ f"Invalid window: start({ self .start } ) is greater than end({ self .end } )"
168
+ )
147
169
148
170
149
171
@dataclass (frozen = True )
0 commit comments