Skip to content

Revert accidental commit #39879

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

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
88 changes: 34 additions & 54 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,66 +225,51 @@ 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)
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)
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
else:
ndims.append(0)
results.append(new_res)

# 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:
# 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()
return concat(results, keys=keys, axis=1, sort=False)
except TypeError as err:

# we are concatting non-NDFrame objects,
Expand All @@ -297,12 +282,7 @@ def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion:
raise ValueError(
"cannot combine transform and aggregation operations"
) from err
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
return result

def agg_dict_like(self, _axis: int) -> FrameOrSeriesUnion:
"""
Expand Down
14 changes: 2 additions & 12 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@ 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 @@ -1125,16 +1124,11 @@ 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 @@ -1145,24 +1139,20 @@ 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"):
print(float_frame.agg(["max", "sqrt"], axis=axis))
float_frame.agg(["max", "sqrt"], axis=axis)

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

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

def test_demo(self):
# demonstration tests
Expand Down