-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
API: rename labels to codes in core/groupby #29402
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
topper-123
merged 3 commits into
pandas-dev:master
from
topper-123:rename_groupby_labels
Nov 7, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
split-apply-combine paradigm. | ||
""" | ||
|
||
from typing import Tuple | ||
from typing import Optional, Tuple | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -21,6 +21,7 @@ | |
) | ||
from pandas.core.dtypes.generic import ABCSeries | ||
|
||
from pandas._typing import FrameOrSeries | ||
import pandas.core.algorithms as algorithms | ||
from pandas.core.arrays import Categorical, ExtensionArray | ||
import pandas.core.common as com | ||
|
@@ -228,10 +229,10 @@ class Grouping: | |
---------- | ||
index : Index | ||
grouper : | ||
obj : | ||
obj Union[DataFrame, Series]: | ||
name : | ||
level : | ||
observed : boolean, default False | ||
observed : bool, default False | ||
If we are a Categorical, use the observed values | ||
in_axis : if the Grouping is a column in self.obj and hence among | ||
Groupby.exclusions list | ||
|
@@ -240,25 +241,22 @@ class Grouping: | |
------- | ||
**Attributes**: | ||
* indices : dict of {group -> index_list} | ||
* labels : ndarray, group labels | ||
* ids : mapping of label -> group | ||
* counts : array of group counts | ||
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. No attributes on |
||
* codes : ndarray, group codes | ||
* group_index : unique groups | ||
* groups : dict of {group -> label_list} | ||
""" | ||
|
||
def __init__( | ||
self, | ||
index, | ||
index: Index, | ||
grouper=None, | ||
obj=None, | ||
obj: Optional[FrameOrSeries] = None, | ||
name=None, | ||
level=None, | ||
sort=True, | ||
observed=False, | ||
in_axis=False, | ||
sort: bool = True, | ||
observed: bool = False, | ||
in_axis: bool = False, | ||
): | ||
|
||
self.name = name | ||
self.level = level | ||
self.grouper = _convert_grouper(index, grouper) | ||
|
@@ -290,12 +288,12 @@ def __init__( | |
if self.name is None: | ||
self.name = index.names[level] | ||
|
||
self.grouper, self._labels, self._group_index = index._get_grouper_for_level( # noqa: E501 | ||
self.grouper, self._codes, self._group_index = index._get_grouper_for_level( # noqa: E501 | ||
self.grouper, level | ||
) | ||
|
||
# a passed Grouper like, directly get the grouper in the same way | ||
# as single grouper groupby, use the group_info to get labels | ||
# as single grouper groupby, use the group_info to get codes | ||
elif isinstance(self.grouper, Grouper): | ||
# get the new grouper; we already have disambiguated | ||
# what key/level refer to exactly, don't need to | ||
|
@@ -308,7 +306,7 @@ def __init__( | |
self.grouper = grouper._get_grouper() | ||
|
||
else: | ||
if self.grouper is None and self.name is not None: | ||
if self.grouper is None and self.name is not None and self.obj is not None: | ||
self.grouper = self.obj[self.name] | ||
|
||
elif isinstance(self.grouper, (list, tuple)): | ||
|
@@ -324,7 +322,7 @@ def __init__( | |
|
||
# we make a CategoricalIndex out of the cat grouper | ||
# preserving the categories / ordered attributes | ||
self._labels = self.grouper.codes | ||
self._codes = self.grouper.codes | ||
if observed: | ||
codes = algorithms.unique1d(self.grouper.codes) | ||
codes = codes[codes != -1] | ||
|
@@ -380,11 +378,11 @@ def __repr__(self): | |
def __iter__(self): | ||
return iter(self.indices) | ||
|
||
_labels = None | ||
_group_index = None | ||
_codes = None # type: np.ndarray | ||
_group_index = None # type: Index | ||
|
||
@property | ||
def ngroups(self): | ||
def ngroups(self) -> int: | ||
return len(self.group_index) | ||
|
||
@cache_readonly | ||
|
@@ -397,38 +395,38 @@ def indices(self): | |
return values._reverse_indexer() | ||
|
||
@property | ||
def labels(self): | ||
if self._labels is None: | ||
self._make_labels() | ||
return self._labels | ||
def codes(self) -> np.ndarray: | ||
if self._codes is None: | ||
self._make_codes() | ||
return self._codes | ||
|
||
@cache_readonly | ||
def result_index(self): | ||
def result_index(self) -> Index: | ||
if self.all_grouper is not None: | ||
return recode_from_groupby(self.all_grouper, self.sort, self.group_index) | ||
return self.group_index | ||
|
||
@property | ||
def group_index(self): | ||
def group_index(self) -> Index: | ||
if self._group_index is None: | ||
self._make_labels() | ||
self._make_codes() | ||
return self._group_index | ||
|
||
def _make_labels(self): | ||
if self._labels is None or self._group_index is None: | ||
def _make_codes(self) -> None: | ||
if self._codes is None or self._group_index is None: | ||
# we have a list of groupers | ||
if isinstance(self.grouper, BaseGrouper): | ||
labels = self.grouper.label_info | ||
codes = self.grouper.codes_info | ||
uniques = self.grouper.result_index | ||
else: | ||
labels, uniques = algorithms.factorize(self.grouper, sort=self.sort) | ||
codes, uniques = algorithms.factorize(self.grouper, sort=self.sort) | ||
uniques = Index(uniques, name=self.name) | ||
self._labels = labels | ||
self._codes = codes | ||
self._group_index = uniques | ||
|
||
@cache_readonly | ||
def groups(self): | ||
return self.index.groupby(Categorical.from_codes(self.labels, self.group_index)) | ||
def groups(self) -> dict: | ||
return self.index.groupby(Categorical.from_codes(self.codes, self.group_index)) | ||
|
||
|
||
def _get_grouper( | ||
|
@@ -678,7 +676,7 @@ def _is_label_like(val): | |
return isinstance(val, (str, tuple)) or (val is not None and is_scalar(val)) | ||
|
||
|
||
def _convert_grouper(axis, grouper): | ||
def _convert_grouper(axis: Index, grouper): | ||
if isinstance(grouper, dict): | ||
return grouper.get | ||
elif isinstance(grouper, Series): | ||
|
Oops, something went wrong.
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.
can you use a list comprehension here