Skip to content

REF: axis in [..] with _get_axis_number() #43151

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
31 changes: 24 additions & 7 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,11 @@ def test_consistency_for_boxed(box, int_frame_const_col):


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

with np.errstate(all="ignore"):

Expand All @@ -1010,7 +1014,7 @@ def test_agg_transform(axis, float_frame):
# list-like
result = float_frame.apply([np.sqrt], axis=axis)
expected = f_sqrt.copy()
if axis in {0, "index"}:
if axis == 0:
expected.columns = MultiIndex.from_product([float_frame.columns, ["sqrt"]])
else:
expected.index = MultiIndex.from_product([float_frame.index, ["sqrt"]])
Expand All @@ -1021,7 +1025,7 @@ def test_agg_transform(axis, float_frame):
# functions per series and then concatting
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"}:
if axis == 0:
expected.columns = MultiIndex.from_product(
[float_frame.columns, ["absolute", "sqrt"]]
)
Expand Down Expand Up @@ -1102,7 +1106,11 @@ def test_agg_multiple_mixed_no_warning():


def test_agg_reduce(axis, float_frame):
other_axis = 1 if axis in {0, "index"} else 0
axis = float_frame._get_axis_number(axis)
if axis == 0:
other_axis = 1
else:
other_axis = 0
name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values()

# all reducers
Expand All @@ -1115,7 +1123,10 @@ def test_agg_reduce(axis, float_frame):
axis=1,
)
expected.columns = ["mean", "max", "sum"]
expected = expected.T if axis in {0, "index"} else expected
if axis == 0:
expected = expected.T
else:
expected = expected

result = float_frame.agg(["mean", "max", "sum"], axis=axis)
tm.assert_frame_equal(result, expected)
Expand All @@ -1141,7 +1152,10 @@ def test_agg_reduce(axis, float_frame):
name2: Series([float_frame.loc(other_axis)[name2].sum()], index=["sum"]),
}
)
expected = expected.T if axis in {1, "columns"} else expected
if axis == 1:
expected = expected.T
else:
expected = expected
tm.assert_frame_equal(result, expected)

# dict input with lists with multiple
Expand All @@ -1166,7 +1180,10 @@ def test_agg_reduce(axis, float_frame):
},
axis=1,
)
expected = expected.T if axis in {1, "columns"} else expected
if axis == 1:
expected = expected.T
else:
expected = expected
tm.assert_frame_equal(result, expected)


Expand Down