-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fixes to FixedForwardWindowIndexer and GroupbyIndexer (#43267) #43291
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
Merged
simonjayhawkins
merged 10 commits into
pandas-dev:master
from
dsm054:repair_fixedforwardwindowindexer
Oct 16, 2021
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
928ba00
BUG: Fixes to FixedForwardWindowIndexer and GroupbyIndexer (#43267)
dsm054 25dc95b
Merge branch 'master' into repair_fixedforwardwindowindexer
jreback dd6ccb2
Undo whitespace changes
mroeschke f609785
Fix type annotation of GroupbyIndexer
mroeschke 2efdfb9
Ignore typing check for window_size since BaseIndexer is public
mroeschke acd9605
Update objects.py
mroeschke eb01640
Merge remote-tracking branch 'upstream/master' into repair_fixedforwa…
mroeschke 27c6c33
Fix typing
mroeschke 6ef4b90
Merge remote-tracking branch 'upstream/master' into repair_fixedforwa…
mroeschke 04851f5
Update per comments
mroeschke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -266,9 +266,8 @@ def get_window_bounds( | |||||
) | ||||||
|
||||||
start = np.arange(num_values, dtype="int64") | ||||||
end_s = start[: -self.window_size] + self.window_size | ||||||
end_e = np.full(self.window_size, num_values, dtype="int64") | ||||||
end = np.concatenate([end_s, end_e]) | ||||||
end = start + self.window_size | ||||||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end[end > num_values] = num_values | ||||||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return start, end | ||||||
|
||||||
|
@@ -279,8 +278,8 @@ class GroupbyIndexer(BaseIndexer): | |||||
def __init__( | ||||||
self, | ||||||
index_array: np.ndarray | None = None, | ||||||
window_size: int = 0, | ||||||
groupby_indicies: dict | None = None, | ||||||
window_size: int | type[BaseIndexer] = 0, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm u can only set the default if this is an int There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and can remove an ignore. |
||||||
groupby_indices: dict | None = None, | ||||||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
window_indexer: type[BaseIndexer] = BaseIndexer, | ||||||
indexer_kwargs: dict | None = None, | ||||||
**kwargs, | ||||||
|
@@ -292,9 +291,9 @@ def __init__( | |||||
np.ndarray of the index of the original object that we are performing | ||||||
a chained groupby operation over. This index has been pre-sorted relative to | ||||||
the groups | ||||||
window_size : int | ||||||
window_size : int or BaseIndexer | ||||||
window size during the windowing operation | ||||||
groupby_indicies : dict or None | ||||||
groupby_indices : dict or None | ||||||
dict of {group label: [positional index of rows belonging to the group]} | ||||||
window_indexer : BaseIndexer | ||||||
BaseIndexer class determining the start and end bounds of each group | ||||||
|
@@ -303,11 +302,13 @@ def __init__( | |||||
**kwargs : | ||||||
keyword arguments that will be available when get_window_bounds is called | ||||||
""" | ||||||
self.groupby_indicies = groupby_indicies or {} | ||||||
self.groupby_indices = groupby_indices or {} | ||||||
self.window_indexer = window_indexer | ||||||
self.indexer_kwargs = indexer_kwargs or {} | ||||||
self.indexer_kwargs = indexer_kwargs.copy() if indexer_kwargs else {} | ||||||
super().__init__( | ||||||
index_array, self.indexer_kwargs.pop("window_size", window_size), **kwargs | ||||||
index_array=index_array, | ||||||
window_size=self.indexer_kwargs.pop("window_size", window_size), | ||||||
**kwargs, | ||||||
) | ||||||
|
||||||
@Appender(get_window_bounds_doc) | ||||||
|
@@ -323,8 +324,8 @@ def get_window_bounds( | |||||
# 3) Append the window bounds in group order | ||||||
start_arrays = [] | ||||||
end_arrays = [] | ||||||
window_indicies_start = 0 | ||||||
for key, indices in self.groupby_indicies.items(): | ||||||
window_indices_start = 0 | ||||||
for key, indices in self.groupby_indices.items(): | ||||||
index_array: np.ndarray | None | ||||||
|
||||||
if self.index_array is not None: | ||||||
|
@@ -341,18 +342,21 @@ def get_window_bounds( | |||||
) | ||||||
start = start.astype(np.int64) | ||||||
end = end.astype(np.int64) | ||||||
# Cannot use groupby_indicies as they might not be monotonic with the object | ||||||
assert len(start) == len( | ||||||
end | ||||||
), "these should be equal in length from get_window_bounds" | ||||||
# Cannot use groupby_indices as they might not be monotonic with the object | ||||||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# we're rolling over | ||||||
window_indicies = np.arange( | ||||||
window_indicies_start, window_indicies_start + len(indices) | ||||||
window_indices = np.arange( | ||||||
window_indices_start, window_indices_start + len(indices) | ||||||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
) | ||||||
window_indicies_start += len(indices) | ||||||
window_indices_start += len(indices) | ||||||
# Extend as we'll be slicing window like [start, end) | ||||||
window_indicies = np.append( | ||||||
window_indicies, [window_indicies[-1] + 1] | ||||||
).astype(np.int64) | ||||||
start_arrays.append(window_indicies.take(ensure_platform_int(start))) | ||||||
end_arrays.append(window_indicies.take(ensure_platform_int(end))) | ||||||
window_indices = np.append(window_indices, [window_indices[-1] + 1]).astype( | ||||||
np.int64, copy=False | ||||||
) | ||||||
start_arrays.append(window_indices.take(ensure_platform_int(start))) | ||||||
end_arrays.append(window_indices.take(ensure_platform_int(end))) | ||||||
start = np.concatenate(start_arrays) | ||||||
end = np.concatenate(end_arrays) | ||||||
return start, end | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. for consistency