Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Add max_lookback to window replace and combine functions #1843

Merged
merged 5 commits into from Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions ibis/expr/tests/test_window_functions.py
Expand Up @@ -63,6 +63,18 @@ def test_combine_windows(alltypes):
with pytest.raises(ibis.common.IbisInputError):
w1.combine(w6)

w7 = ibis.trailing_window(
rows_with_max_lookback(3, ibis.interval(days=5))
)
w8 = ibis.trailing_window(
rows_with_max_lookback(5, ibis.interval(days=7))
)
w9 = w7.combine(w8)
expected = ibis.trailing_window(
rows_with_max_lookback(3, ibis.interval(days=5))
)
assert_equal(w9, expected)


def test_over_auto_bind(alltypes):
# GH #542
Expand Down
6 changes: 5 additions & 1 deletion ibis/expr/window.py
Expand Up @@ -88,6 +88,7 @@ def __init__(
order_by=None,
preceding=None,
following=None,
max_lookback=None,
how='rows',
):
if group_by is None:
Expand All @@ -111,7 +112,7 @@ def __init__(
self.max_lookback = preceding.max_lookback
else:
self.preceding = _sequence_to_tuple(preceding)
self.max_lookback = None
self.max_lookback = max_lookback

self.following = _sequence_to_tuple(following)
self.how = how
Expand Down Expand Up @@ -227,9 +228,11 @@ def combine(self, window):
"Expecting '{}' Window, got '{}'"
).format(self.how.upper(), window.how.upper())
)
mlb = self.max_lookback
kwds = dict(
preceding=self.preceding or window.preceding,
following=self.following or window.following,
max_lookback=mlb if mlb is not None else window.max_lookback,
group_by=self._group_by + window._group_by,
order_by=self._order_by + window._order_by,
)
Expand All @@ -245,6 +248,7 @@ def _replace(self, **kwds):
order_by=kwds.get('order_by', self._order_by),
preceding=kwds.get('preceding', self.preceding),
following=kwds.get('following', self.following),
max_lookback=kwds.get('max_lookback', self.max_lookback),
emilyreff7 marked this conversation as resolved.
Show resolved Hide resolved
how=kwds.get('how', self.how),
)
return Window(**new_kwds)
Expand Down