Skip to content

Commit c208bbe

Browse files
jbrockmendelBlake Hawkins
authored and
Blake Hawkins
committed
BUG: Fix TypeError in _cython_agg_blocks (pandas-dev#29035)
1 parent 4d27ef2 commit c208bbe

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

pandas/core/groupby/generic.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,11 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
970970

971971
# call our grouper again with only this block
972972
obj = self.obj[data.items[locs]]
973+
if obj.shape[1] == 1:
974+
# Avoid call to self.values that can occur in DataFrame
975+
# reductions; see GH#28949
976+
obj = obj.iloc[:, 0]
977+
973978
s = groupby(obj, self.grouper)
974979
try:
975980
result = s.aggregate(lambda x: alt(x, axis=self.axis))
@@ -978,17 +983,29 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
978983
# continue and exclude the block
979984
deleted_items.append(locs)
980985
continue
986+
987+
# unwrap DataFrame to get array
988+
assert len(result._data.blocks) == 1
989+
result = result._data.blocks[0].values
990+
if result.ndim == 1 and isinstance(result, np.ndarray):
991+
result = result.reshape(1, -1)
992+
981993
finally:
994+
assert not isinstance(result, DataFrame)
995+
982996
if result is not no_result:
983997
# see if we can cast the block back to the original dtype
984998
result = maybe_downcast_numeric(result, block.dtype)
985999

986-
if result.ndim == 1 and isinstance(result, np.ndarray):
1000+
if block.is_extension and isinstance(result, np.ndarray):
9871001
# e.g. block.values was an IntegerArray
1002+
# (1, N) case can occur if block.values was Categorical
1003+
# and result is ndarray[object]
1004+
assert result.ndim == 1 or result.shape[0] == 1
9881005
try:
9891006
# Cast back if feasible
9901007
result = type(block.values)._from_sequence(
991-
result, dtype=block.values.dtype
1008+
result.ravel(), dtype=block.values.dtype
9921009
)
9931010
except ValueError:
9941011
# reshape to be valid for non-Extension Block

pandas/core/groupby/groupby.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1344,13 +1344,14 @@ def f(self, **kwargs):
13441344
raise
13451345
except DataError:
13461346
pass
1347-
except Exception:
1348-
# TODO: the remaining test cases that get here are from:
1349-
# - AttributeError from _cython_agg_blocks bug passing
1350-
# DataFrame to make_block; see GH#28275
1351-
# - TypeError in _cython_operation calling ensure_float64
1352-
# on object array containing complex numbers;
1353-
# see test_groupby_complex, test_max_nan_bug
1347+
except (TypeError, NotImplementedError):
1348+
# TODO:
1349+
# - TypeError: this is reached via test_groupby_complex
1350+
# and can be fixed by implementing _group_add for
1351+
# complex dtypes
1352+
# - NotImplementedError: reached in test_max_nan_bug,
1353+
# raised in _get_cython_function and should probably
1354+
# be handled inside _cython_agg_blocks
13541355
pass
13551356

13561357
# apply a non-cython aggregation

0 commit comments

Comments
 (0)