Skip to content

API: reimplement FixedWindowIndexer.get_window_bounds to fix groupby bug #36132

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
69f084f
updated fixed indexer to work with rolling df and groupby
justinessert Sep 4, 2020
71830c8
updated is_weighted case
justinessert Sep 4, 2020
a449d9b
added comment
justinessert Sep 4, 2020
476fe83
corrected offset for even window sizes
justinessert Sep 5, 2020
9dfd9f3
reverted changes for weighted windows
justinessert Sep 5, 2020
f025600
reverted back to fixed func type; added func_type variable
justinessert Sep 6, 2020
5d902fd
reformatted
justinessert Sep 6, 2020
59fcd3e
merged master and resolved conflict
justinessert Sep 6, 2020
cdecf34
corrected return typing
justinessert Sep 6, 2020
4e8f844
added consistency tests
justinessert Sep 6, 2020
6e66a49
corrected typing change
justinessert Sep 6, 2020
e7fb384
reformatted test to pass blac
justinessert Sep 6, 2020
3649ca2
added typing and docstring
justinessert Sep 6, 2020
00cc1dc
fixing center param in median's _apply
justinessert Sep 6, 2020
3de7fcc
added center_min_periods test to test_grouper
justinessert Sep 7, 2020
f779321
replaced func_type with skip_offset
justinessert Sep 9, 2020
a817f87
removed unneeded class attribute
justinessert Sep 9, 2020
d72812d
moved logic into calculate_center_offset
justinessert Sep 9, 2020
96c6959
removed whitespace
justinessert Sep 9, 2020
daacae7
added whatsnew entry
justinessert Sep 12, 2020
52a8a6b
Merge remote-tracking branch 'upstream/master' into groupby-rolling
justinessert Sep 12, 2020
950018c
formatting fixes
justinessert Sep 13, 2020
0798c70
removed pytest.slow
justinessert Sep 13, 2020
f413ec8
removed typing of window
justinessert Sep 13, 2020
70679be
Merge branch 'master' into groupby-rolling
justinessert Sep 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions pandas/core/window/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ def get_window_bounds(
closed: Optional[str] = None,
) -> Tuple[np.ndarray, np.ndarray]:

start_s = np.zeros(self.window_size, dtype="int64")
start_e = (
np.arange(self.window_size, num_values, dtype="int64")
- self.window_size
+ 1
)
start = np.concatenate([start_s, start_e])[:num_values]
if center:
offset = self.window_size // 2
else:
offset = 0

end = np.arange(1 + offset, num_values + 1 + offset)
start = end - self.window_size

# end is exclusive, whereas start is inclusive
# thus the bounds for end should be 1 greater than the bounds for start
end = np.clip(end, 1, num_values)
start = np.clip(start, 0, num_values - 1)

end_s = np.arange(self.window_size, dtype="int64") + 1
end_e = start_e + self.window_size
end = np.concatenate([end_s, end_e])[:num_values]
return start, end


Expand Down
12 changes: 1 addition & 11 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,7 @@ def _get_cython_func_type(self, func: str) -> Callable:

Variable algorithms do not use window while fixed do.
"""
if self.is_freq_type or isinstance(self.window, BaseIndexer):
return self._get_roll_func(f"{func}_variable")
return partial(self._get_roll_func(f"{func}_fixed"), win=self._get_window())
return self._get_roll_func(f"{func}_variable")

def _get_window_indexer(self, window: int) -> BaseIndexer:
"""
Expand Down Expand Up @@ -611,13 +609,9 @@ def homogeneous_func(values: np.ndarray):
if values.size == 0:
return values.copy()

offset = calculate_center_offset(window) if center else 0
additional_nans = np.array([np.nan] * offset)

if not is_weighted:

def calc(x):
x = np.concatenate((x, additional_nans))
if not isinstance(self.window, BaseIndexer):
min_periods = calculate_min_periods(
window, self.min_periods, len(x), require_min_periods, floor
Expand All @@ -641,7 +635,6 @@ def calc(x):
else:

def calc(x):
x = np.concatenate((x, additional_nans))
return func(x, window, self.min_periods)

with np.errstate(all="ignore"):
Expand All @@ -654,9 +647,6 @@ def calc(x):
if use_numba_cache:
NUMBA_FUNC_CACHE[(kwargs["original_func"], "rolling_apply")] = func

if center:
result = self._center_window(result, window)

return result

return self._apply_blockwise(homogeneous_func)
Expand Down