Skip to content
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

[SNOW-1478924]: Ensure __getitem__ on DataFrameGroupBy returns SeriesGroupBy when appropriate even if as_index=False #2475

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Fixed a bug where `replace()` would sometimes propagate `Timedelta` types incorrectly through `replace()`. Instead raise `NotImplementedError` for `replace()` on `Timedelta`.
- Fixed a bug where `DataFrame` and `Series` `round()` would raise `AssertionError` for `Timedelta` columns. Instead raise `NotImplementedError` for `round()` on `Timedelta`.
- Fixed a bug where `reindex` fails when the new index is a Series with non-overlapping types from the original index.
- Fixed a bug where calling `__getitem__` on a DataFrameGroupBy object always returned a DataFrameGroupBy object if `as_index=False`.


## 1.23.0 (2024-10-09)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,20 +1082,18 @@ def __getitem__(self, key):
"idx_name": self._idx_name,
}
# The rules of type deduction for the resulted object is the following:
# 1. If `key` is a list-like or `as_index is False`, then the resulted object is a DataFrameGroupBy
# 1. If `key` is a list-like, then the resulted object is a DataFrameGroupBy
# 2. Otherwise, the resulted object is SeriesGroupBy
# 3. Result type does not depend on the `by` origin
# Examples:
# - drop: any, as_index: any, __getitem__(key: list_like) -> DataFrameGroupBy
# - drop: any, as_index: False, __getitem__(key: any) -> DataFrameGroupBy
# - drop: any, as_index: False, __getitem__(key: list_like) -> DataFrameGroupBy
# - drop: any, as_index: False, __getitem__(key: label) -> SeriesGroupBy
# - drop: any, as_index: True, __getitem__(key: label) -> SeriesGroupBy
if is_list_like(key):
make_dataframe = True
else:
if self._as_index:
make_dataframe = False
else:
make_dataframe = True
make_dataframe = False
key = [key]

column_index = self._df.columns
Expand Down Expand Up @@ -1267,7 +1265,12 @@ def _wrap_aggregation(
numeric_only = False

if is_result_dataframe is None:
is_result_dataframe = not is_series_groupby
# If the GroupBy object is a SeriesGroupBy, we generally return a Series
# after an aggregation - unless `as_index` is False, in which case we
# return a DataFrame with N columns, where the first N-1 columns are
# the grouping columns (by), and the Nth column is the aggregation
# result.
is_result_dataframe = not is_series_groupby or not self._as_index
result_type = pd.DataFrame if is_result_dataframe else pd.Series
result = result_type(
query_compiler=qc_method(
Expand Down Expand Up @@ -1427,7 +1430,11 @@ def unique(self):

def size(self):
# TODO: Remove this once SNOW-1478924 is fixed
return super().size().rename(self._df.columns[-1])
result = super().size()
if isinstance(result, Series):
return result.rename(self._df.columns[-1])
else:
return result

def value_counts(
self,
Expand Down
Loading