-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.groupby(., dropna=True, axis=0) incorrectly throws ShapeError #35751
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
Changes from 11 commits
0484244
a335744
640ec38
099e30c
8a13d06
394feb6
e1cafd4
0df329c
77e7fc7
16544ea
46e5f66
6fca785
6341d97
269516b
6819ac6
2ec491d
249fc2a
b1cafad
bef1437
9abc8c4
c63a24c
9791e1e
21a6fbb
8afb6e2
239e16a
0cdea22
ee73640
c07df76
ca2f898
e15df1a
342540f
90e687b
a10a933
bddfa81
2adba09
531414f
10ee18a
2fcfda0
1969bc4
2972ee4
62caeb6
557903f
deb1b09
3d579c5
ec85d7f
f6a9724
bfe6cde
b6fd41c
983bb8e
1884133
f207709
4422a21
daa60a6
5658c12
4326b79
e12e8d9
9e6a130
1770cc2
6a005dc
74dbe4f
0e9db9c
8be535c
91940c7
85d2165
c31b49f
21bfc82
7f67086
5555585
5ac7fbf
1e7ab91
96b5af4
9cf9e05
f5a1635
bd1abf9
a789b6a
2ba8b44
95b86ba
7881134
15aa56e
08f0abd
8ab9baa
faf6570
7bd2a9a
24bb112
4377b63
de86144
9bc9ce4
1ea9d29
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 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -538,14 +538,20 @@ def _transform_general( | |||||||||||||||||||||||||||
if isinstance(res, (ABCDataFrame, ABCSeries)): | ||||||||||||||||||||||||||||
res = res._values | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
results.append(klass(res, index=group.index)) | ||||||||||||||||||||||||||||
indexer = self._get_index(name) if self.dropna else group.index | ||||||||||||||||||||||||||||
results.append(klass(res, index=indexer)) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# check for empty "results" to avoid concat ValueError | ||||||||||||||||||||||||||||
if results: | ||||||||||||||||||||||||||||
from pandas.core.reshape.concat import concat | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
concatenated = concat(results) | ||||||||||||||||||||||||||||
result = self._set_result_index_ordered(concatenated) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if not self.dropna: | ||||||||||||||||||||||||||||
result = self._set_result_index_ordered(concatenated) | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
result = concatenated.sort_index() | ||||||||||||||||||||||||||||
result.index = self._selected_obj.index[result.index.asi8] | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
result = self.obj._constructor(dtype=np.float64) | ||||||||||||||||||||||||||||
# we will only try to coerce the result type if | ||||||||||||||||||||||||||||
|
@@ -557,7 +563,6 @@ def _transform_general( | |||||||||||||||||||||||||||
result = maybe_downcast_numeric(result, self._selected_obj.dtype) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
result.name = self._selected_obj.name | ||||||||||||||||||||||||||||
result.index = self._selected_obj.index | ||||||||||||||||||||||||||||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def _transform_fast(self, result) -> Series: | ||||||||||||||||||||||||||||
|
@@ -1392,6 +1397,9 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False): | |||||||||||||||||||||||||||
def _transform_general( | ||||||||||||||||||||||||||||
arw2019 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
self, func, *args, engine="cython", engine_kwargs=None, **kwargs | ||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
Transform with a non-str `func`. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
from pandas.core.reshape.concat import concat | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
applied = [] | ||||||||||||||||||||||||||||
|
@@ -1404,7 +1412,9 @@ def _transform_general( | |||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
fast_path, slow_path = self._define_paths(func, *args, **kwargs) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
has_nan = False | ||||||||||||||||||||||||||||
for name, group in gen: | ||||||||||||||||||||||||||||
has_nan = has_nan or isna(name) | ||||||||||||||||||||||||||||
object.__setattr__(group, "name", name) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if maybe_use_numba(engine): | ||||||||||||||||||||||||||||
|
@@ -1413,9 +1423,8 @@ def _transform_general( | |||||||||||||||||||||||||||
if cache_key not in NUMBA_FUNC_CACHE: | ||||||||||||||||||||||||||||
NUMBA_FUNC_CACHE[cache_key] = numba_func | ||||||||||||||||||||||||||||
# Return the result as a DataFrame for concatenation later | ||||||||||||||||||||||||||||
res = self.obj._constructor( | ||||||||||||||||||||||||||||
res, index=group.index, columns=group.columns | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
indexer = self._get_index(name) if self.dropna else group.index | ||||||||||||||||||||||||||||
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. i think you can arrange to have dropna passed to where the groups are constructed, then you won't need to do this as group.index will be correct. 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. So the groups are constructed in this pandas/pandas/core/groupby/ops.py Lines 116 to 128 in 361166f
I think that BaseGrouper doesn't have a dropna attribute - I checked inside this method with hasattr(self, 'dropna') to be sure. Would the idea be to propagate dropna to BaseGrouper so that group.index is correct?
Not sure this is the way to go but thought I'd ask |
||||||||||||||||||||||||||||
res = self.obj._constructor(res, index=indexer, columns=group.columns) | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
# Try slow path and fast path. | ||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||
|
@@ -1449,7 +1458,6 @@ def _transform_general( | |||||||||||||||||||||||||||
applied.append(r) | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
applied.append(res) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
concat_index = obj.columns if self.axis == 0 else obj.index | ||||||||||||||||||||||||||||
other_axis = 1 if self.axis == 0 else 0 # switches between 0 & 1 | ||||||||||||||||||||||||||||
concatenated = concat(applied, axis=self.axis, verify_integrity=False) | ||||||||||||||||||||||||||||
|
@@ -1677,12 +1685,17 @@ def _gotitem(self, key, ndim: int, subset=None): | |||||||||||||||||||||||||||
exclusions=self.exclusions, | ||||||||||||||||||||||||||||
as_index=self.as_index, | ||||||||||||||||||||||||||||
observed=self.observed, | ||||||||||||||||||||||||||||
dropna=self.dropna, | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
elif ndim == 1: | ||||||||||||||||||||||||||||
if subset is None: | ||||||||||||||||||||||||||||
subset = self.obj[key] | ||||||||||||||||||||||||||||
return SeriesGroupBy( | ||||||||||||||||||||||||||||
subset, selection=key, grouper=self.grouper, observed=self.observed | ||||||||||||||||||||||||||||
subset, | ||||||||||||||||||||||||||||
selection=key, | ||||||||||||||||||||||||||||
grouper=self.grouper, | ||||||||||||||||||||||||||||
observed=self.observed, | ||||||||||||||||||||||||||||
dropna=self.dropna, | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
raise AssertionError("invalid ndim for _gotitem") | ||||||||||||||||||||||||||||
|
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.
missing values in the grouper? with dropna?
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.
rewrote, mentioned grouper. Also separated this entry from #35014