-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: groupby #29626
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
CLN: groupby #29626
Changes from 3 commits
8234eac
efc6701
836dc1a
f882996
a91a489
5ff6abb
2c67e86
57e0b91
7fcda06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,7 +334,9 @@ def _aggregate_multiple_funcs(self, arg, _level): | |
|
||
return DataFrame(results, columns=columns) | ||
|
||
def _wrap_series_output(self, output, index, names=None): | ||
def _wrap_series_output( | ||
self, output: dict, index: Index, names=None | ||
) -> Union[Series, DataFrame]: | ||
""" common agg/transform wrapping logic """ | ||
output = output[self._selection_name] | ||
|
||
|
@@ -346,18 +348,22 @@ def _wrap_series_output(self, output, index, names=None): | |
name = self._selected_obj.name | ||
return Series(output, index=index, name=name) | ||
|
||
def _wrap_aggregated_output(self, output, names=None): | ||
def _wrap_aggregated_output( | ||
self, output: dict, names=None | ||
) -> Union[Series, DataFrame]: | ||
result = self._wrap_series_output( | ||
output=output, index=self.grouper.result_index, names=names | ||
) | ||
return self._reindex_output(result)._convert(datetime=True) | ||
|
||
def _wrap_transformed_output(self, output, names=None): | ||
def _wrap_transformed_output( | ||
self, output: dict, names=None | ||
) -> Union[Series, DataFrame]: | ||
return self._wrap_series_output( | ||
output=output, index=self.obj.index, names=names | ||
) | ||
|
||
def _wrap_applied_output(self, keys, values, not_indexed_same=False): | ||
def _wrap_applied_output(self, keys, values, not_indexed_same: bool = False): | ||
if len(keys) == 0: | ||
# GH #6265 | ||
return Series([], name=self._selection_name, index=keys) | ||
|
@@ -389,8 +395,8 @@ def _get_index() -> Index: | |
result = Series(data=values, index=_get_index(), name=self._selection_name) | ||
return self._reindex_output(result) | ||
|
||
def _aggregate_named(self, func, *args, **kwargs): | ||
result = OrderedDict() | ||
def _aggregate_named(self, func, *args, **kwargs) -> OrderedDict: | ||
result = OrderedDict() # type: OrderedDict | ||
|
||
for name, group in self: | ||
group.name = name | ||
|
@@ -455,18 +461,16 @@ def transform(self, func, *args, **kwargs): | |
result.index = self._selected_obj.index | ||
return result | ||
|
||
def _transform_fast(self, func, func_nm) -> Series: | ||
def _transform_fast(self, func: Callable, func_nm: str) -> Series: | ||
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. If the arguments and/or return type of |
||
""" | ||
fast version of transform, only applicable to | ||
builtin/cythonizable functions | ||
""" | ||
if isinstance(func, str): | ||
func = getattr(self, func) | ||
|
||
ids, _, ngroup = self.grouper.group_info | ||
cast = self._transform_should_cast(func_nm) | ||
should_cast = self._transform_should_cast(func_nm) | ||
out = algorithms.take_1d(func()._values, ids) | ||
if cast: | ||
if should_cast: | ||
out = self._try_cast(out, self.obj) | ||
return Series(out, index=self.obj.index, name=self.obj.name) | ||
|
||
|
@@ -591,7 +595,12 @@ def describe(self, **kwargs): | |
return result.unstack() | ||
|
||
def value_counts( | ||
self, normalize=False, sort=True, ascending=False, bins=None, dropna=True | ||
self, | ||
normalize: bool = False, | ||
sort: bool = True, | ||
ascending: bool = False, | ||
bins=None, | ||
dropna: bool = True, | ||
): | ||
|
||
from pandas.core.reshape.tile import cut | ||
|
@@ -1081,6 +1090,7 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame: | |
def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame: | ||
# only for axis==0 | ||
|
||
should_cast = self._transform_should_cast(func) | ||
obj = self._obj_with_exclusions | ||
result = OrderedDict() # type: dict | ||
cannot_agg = [] | ||
|
@@ -1089,9 +1099,8 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame: | |
data = obj[item] | ||
colg = SeriesGroupBy(data, selection=item, grouper=self.grouper) | ||
|
||
cast = self._transform_should_cast(func) | ||
try: | ||
result[item] = colg.aggregate(func, *args, **kwargs) | ||
res = colg.aggregate(func, *args, **kwargs) | ||
|
||
except ValueError as err: | ||
if "Must produce aggregated value" in str(err): | ||
|
@@ -1103,8 +1112,9 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame: | |
continue | ||
|
||
else: | ||
if cast: | ||
result[item] = self._try_cast(result[item], data) | ||
if should_cast: | ||
res = self._try_cast(res, data) | ||
result[item] = res | ||
|
||
result_columns = obj.columns | ||
if cannot_agg: | ||
|
@@ -1127,7 +1137,7 @@ def _decide_output_index(self, output, labels): | |
|
||
return output_keys | ||
|
||
def _wrap_applied_output(self, keys, values, not_indexed_same=False): | ||
def _wrap_applied_output(self, keys, values, not_indexed_same: bool = False): | ||
if len(keys) == 0: | ||
return DataFrame(index=keys) | ||
|
||
|
@@ -1379,21 +1389,23 @@ def transform(self, func, *args, **kwargs): | |
|
||
return self._transform_fast(result, obj, func) | ||
|
||
def _transform_fast(self, result: DataFrame, obj: DataFrame, func_nm) -> DataFrame: | ||
def _transform_fast( | ||
self, result: DataFrame, obj: DataFrame, func_nm: str | ||
) -> DataFrame: | ||
""" | ||
Fast transform path for aggregations | ||
""" | ||
# if there were groups with no observations (Categorical only?) | ||
# try casting data to original dtype | ||
cast = self._transform_should_cast(func_nm) | ||
should_cast = self._transform_should_cast(func_nm) | ||
|
||
# for each col, reshape to to size of original frame | ||
# by take operation | ||
ids, _, ngroup = self.grouper.group_info | ||
output = [] | ||
for i, _ in enumerate(result.columns): | ||
res = algorithms.take_1d(result.iloc[:, i].values, ids) | ||
if cast: | ||
if should_cast: | ||
res = self._try_cast(res, obj.iloc[:, i]) | ||
output.append(res) | ||
|
||
|
@@ -1591,7 +1603,7 @@ def _insert_inaxis_grouper_inplace(self, result): | |
if in_axis: | ||
result.insert(0, name, lev) | ||
|
||
def _wrap_aggregated_output(self, output, names=None): | ||
def _wrap_aggregated_output(self, output: dict, names=None): | ||
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. dict -> typing.Dict and ideally with subtypes if possible (here and below) |
||
agg_axis = 0 if self.axis == 1 else 1 | ||
agg_labels = self._obj_with_exclusions._get_axis(agg_axis) | ||
|
||
|
@@ -1610,7 +1622,7 @@ def _wrap_aggregated_output(self, output, names=None): | |
|
||
return self._reindex_output(result)._convert(datetime=True) | ||
|
||
def _wrap_transformed_output(self, output, names=None) -> DataFrame: | ||
def _wrap_transformed_output(self, output: dict, names=None) -> DataFrame: | ||
return DataFrame(output, index=self.obj.index) | ||
|
||
def _wrap_agged_blocks(self, items, blocks): | ||
|
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 just be a dict now