-
- Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG:Time Grouper bug fix when applied for list groupers #17587
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
Merged
Changes from 1 commit
Commits
Show all changes
10 commits Select commit Hold shift + click to select a range
6838d9c use grouper if given the list if grouper
ruiann 66febe4 BinGrouper holds the sorted binners, give the indexer to reorder axis…
ruiann 0ff386f PEP8 fix
ruiann 00a18e3 PEP8 & comment
ruiann cc1a4ac comment
ruiann 6927207 new method for BaseGrouper and inherited class
ruiann 3ce56bd comment
ruiann 737c00e move test to test time grouper
ruiann b868cbc update whatsnew entry
ruiann 950de20 fixups
jreback 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
Prev Previous commit
fixups
- Loading branch information
commit 950de2031bad7a88c29bac9e86a73e1e7b2a495d
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -256,11 +256,13 @@ def __init__(self, key=None, level=None, freq=None, axis=0, sort=False): | |
| def ax(self): | ||
| return self.grouper | ||
| | ||
| def _get_grouper(self, obj): | ||
| def _get_grouper(self, obj, validate=True): | ||
| """ | ||
| Parameters | ||
| ---------- | ||
| obj : the subject object | ||
| validate : boolean, default True | ||
| if True, validate the grouper | ||
| | ||
| Returns | ||
| ------- | ||
| | @@ -271,7 +273,8 @@ def _get_grouper(self, obj): | |
| self.grouper, exclusions, self.obj = _get_grouper(self.obj, [self.key], | ||
| axis=self.axis, | ||
| level=self.level, | ||
| sort=self.sort) | ||
| sort=self.sort, | ||
| validate=validate) | ||
| return self.binner, self.grouper, self.obj | ||
| | ||
| def _set_grouper(self, obj, sort=False): | ||
| | @@ -1739,8 +1742,9 @@ class BaseGrouper(object): | |
| whether this grouper will give sorted result or not | ||
| group_keys : boolean, default True | ||
| mutated : boolean, default False | ||
| indexer : the indexer created by Grouper | ||
| some grouper (TimeGrouper eg) will sort its axis and its | ||
| indexer : intp array, optional | ||
| the indexer created by Grouper | ||
| some groupers (TimeGrouper) will sort its axis and its | ||
| group_info is also sorted, so need the indexer to reorder | ||
| | ||
| """ | ||
| | @@ -2514,8 +2518,11 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None, | |
| # a passed Grouper like, directly get the grouper in the same way | ||
| # as single grouper groupby, use the group_info to get labels | ||
| elif isinstance(self.grouper, Grouper): | ||
| # get the new grouper | ||
| _, grouper, _ = self.grouper._get_grouper(self.obj) | ||
| # get the new grouper; we already have disambiguated | ||
| # what key/level refer to exactly, don't need to | ||
| # check again as we have by this point converted these | ||
| # to an actual value (rather than a pd.Grouper) | ||
| _, grouper, _ = self.grouper._get_grouper(self.obj, validate=False) | ||
| if self.name is None: | ||
| self.name = grouper.result_index.name | ||
| self.obj = self.grouper.obj | ||
| | @@ -2587,12 +2594,12 @@ def ngroups(self): | |
| | ||
| @cache_readonly | ||
| def indices(self): | ||
| # for the situation of groupby list of groupers | ||
| # we have a list of groupers | ||
| if isinstance(self.grouper, BaseGrouper): | ||
| return self.grouper.indices | ||
| else: | ||
| values = _ensure_categorical(self.grouper) | ||
| return values._reverse_indexer() | ||
| | ||
| values = _ensure_categorical(self.grouper) | ||
| return values._reverse_indexer() | ||
| | ||
| @property | ||
| def labels(self): | ||
| | @@ -2608,7 +2615,7 @@ def group_index(self): | |
| | ||
| def _make_labels(self): | ||
| if self._labels is None or self._group_index is None: | ||
| # for the situation of groupby list of groupers | ||
| # we have a list of groupers | ||
| if isinstance(self.grouper, BaseGrouper): | ||
| Contributor 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. same | ||
| labels = self.grouper.label_info | ||
| uniques = self.grouper.result_index | ||
| | @@ -2626,7 +2633,7 @@ def groups(self): | |
| | ||
| | ||
| def _get_grouper(obj, key=None, axis=0, level=None, sort=True, | ||
| mutated=False): | ||
| mutated=False, validate=True): | ||
| """ | ||
| create and return a BaseGrouper, which is an internal | ||
| mapping of how to create the grouper indexers. | ||
| | @@ -2643,6 +2650,8 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True, | |
| are and then creates a Grouping for each one, combined into | ||
| a BaseGrouper. | ||
| | ||
| If validate, then check for key/level overlaps | ||
| | ||
| """ | ||
| group_axis = obj._get_axis(axis) | ||
| | ||
| | @@ -2767,7 +2776,7 @@ def is_in_obj(gpr): | |
| | ||
| elif is_in_axis(gpr): # df.groupby('name') | ||
| if gpr in obj: | ||
| if gpr in obj.index.names: | ||
| if validate and gpr in obj.index.names: | ||
| warnings.warn( | ||
| ("'%s' is both a column name and an index level.\n" | ||
| "Defaulting to column but " | ||
| | ||
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.
when is this hit?