Skip to content

Commit

Permalink
fixup for groupby
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach committed Feb 18, 2021
1 parent 0176228 commit 3d281cf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
88 changes: 54 additions & 34 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,51 +225,66 @@ def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion:

results = []
keys = []
ndims = []

# degenerate case
if selected_obj.ndim == 1:
for a in arg:
colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj)
try:
new_res = colg.aggregate(a)

except TypeError:
pass
# if selected_obj.ndim == 1:
for a in arg:
# colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj)
try:
# new_res = colg.aggregate(a)
print('selected_obj:', type(selected_obj))
print(selected_obj)
print('###')
new_res = selected_obj.aggregate(a)
print(new_res)

except TypeError:
pass
else:
results.append(new_res)
if isinstance(new_res, ABCNDFrame):
ndims.append(new_res.ndim)
else:
results.append(new_res)
ndims.append(0)

# make sure we find a good name
name = com.get_callable_name(a) or a
keys.append(name)
# make sure we find a good name
name = com.get_callable_name(a) or a
keys.append(name)

# multiples
else:
for index, col in enumerate(selected_obj):
colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index])
try:
new_res = colg.aggregate(arg)
except (TypeError, DataError):
pass
except ValueError as err:
# cannot aggregate
if "Must produce aggregated value" in str(err):
# raised directly in _aggregate_named
pass
elif "no results" in str(err):
# raised directly in _aggregate_multiple_funcs
pass
else:
raise
else:
results.append(new_res)
keys.append(col)
# else:
# for index, col in enumerate(selected_obj):
# colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index])
# try:
# new_res = colg.aggregate(arg)
# except (TypeError, DataError):
# pass
# except ValueError as err:
# # cannot aggregate
# if "Must produce aggregated value" in str(err):
# # raised directly in _aggregate_named
# pass
# elif "no results" in str(err):
# # raised directly in _aggregate_multiple_funcs
# pass
# else:
# raise
# else:
# results.append(new_res)
# keys.append(col)

# if we are empty
if not len(results):
raise ValueError("no results")

try:
return concat(results, keys=keys, axis=1, sort=False)
# if len(results) == 0:
# result = results[0]
# else:
result = concat(results, keys=keys, axis=1, sort=False)
if all([e == 1 for e in ndims]):
result = result.T.infer_objects()
except TypeError as err:

# we are concatting non-NDFrame objects,
Expand All @@ -282,7 +297,12 @@ def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion:
raise ValueError(
"cannot combine transform and aggregation operations"
) from err
return result
else:
if result.columns.nlevels > 1:
new_order = [-1] + list(range(result.columns.nlevels - 1))
result = result.reorder_levels(new_order, axis='columns')
result = result[selected_obj.columns]
return result

def agg_dict_like(self, _axis: int) -> FrameOrSeriesUnion:
"""
Expand Down
14 changes: 12 additions & 2 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ def test_consistency_for_boxed(self, box, int_frame_const_col):

class TestDataFrameAggregate:
def test_agg_transform(self, axis, float_frame):
float_frame = float_frame.head()
other_axis = 1 if axis in {0, "index"} else 0

with np.errstate(all="ignore"):
Expand All @@ -1124,11 +1125,16 @@ def test_agg_transform(self, axis, float_frame):
expected.index = pd.MultiIndex.from_product(
[float_frame.index, ["sqrt"]]
)
print("result")
print(result)
print('expected')
print(expected)
tm.assert_frame_equal(result, expected)

# multiple items in list
# these are in the order as if we are applying both
# functions per series and then concatting
print('marker')
result = float_frame.apply([np.abs, np.sqrt], axis=axis)
expected = zip_frames([f_abs, f_sqrt], axis=other_axis)
if axis in {0, "index"}:
Expand All @@ -1139,20 +1145,24 @@ def test_agg_transform(self, axis, float_frame):
expected.index = pd.MultiIndex.from_product(
[float_frame.index, ["absolute", "sqrt"]]
)
print()
print(result)
print()
print(expected)
tm.assert_frame_equal(result, expected)

def test_transform_and_agg_err(self, axis, float_frame):
# cannot both transform and agg
msg = "cannot combine transform and aggregation operations"
with pytest.raises(ValueError, match=msg):
with np.errstate(all="ignore"):
float_frame.agg(["max", "sqrt"], axis=axis)
print(float_frame.agg(["max", "sqrt"], axis=axis))

df = DataFrame({"A": range(5), "B": 5})

def f():
with np.errstate(all="ignore"):
df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis)
print(df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis))

def test_demo(self):
# demonstration tests
Expand Down

0 comments on commit 3d281cf

Please sign in to comment.