Skip to content

ENH: Add axis=None for statistical methods #46815

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 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions pandas/core/generic.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10671,6 +10671,23 @@ def _stat_function(

validate_bool_kwarg(skipna, "skipna", none_allowed=False)

if axis is None and level is None and self.ndim > 1 and numeric_only:
# user must have explictly passed axis=None and numeric_only=True
# Converting to numpy array since using numpy statistical methods
arr = self.to_numpy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with EA dtypes?

if name == "max":
if skipna:
return arr.max(axis=axis)
elif name == "min":
if skipna:
return arr.min(axis=axis)
elif name == "mean":
if skipna:
return arr.mean(axis=axis)
elif name == "median":
if skipna:
return np.median(arr, axis=axis)

if axis is None and level is None and self.ndim > 1:
# user must have explicitly passed axis=None
# GH#21597
Expand Down
136 changes: 136 additions & 0 deletions pandas/tests/frame/test_methods_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""Test For axis=None."""
from pandas import (
DataFrame,
)


class TestDataFrameNone:
"""TestDataFrameReductions class."""
def test_any_all_int(self):
"""Other methods should model this behavior."""
# DataFrame consists of ints
df_1 = DataFrame(
{
"foo1": [1, 10, 15],
"foo": [4, 5, 6]
}
)

res = df_1.any(axis=None)
exp = True
assert res == exp

# DataFrame consists of ints
df_2 = DataFrame(
{
"foo1": [2, 4, 1],
"foo": [1, 1, 0]
}
)

res = df_2.all(axis=None)
exp = False
assert res == exp

def test_min_max_int(self):
"""Testing methods min and max."""
# DataFrame consists of ints
df_1 = DataFrame(
{
"foo1": [1, 10, 15],
"foo": [4, 5, 6]
}
)

res = df_1.max(axis=None, numeric_only=True)
exp = 15
assert res == exp

# DataFrame consists of ints
df_2 = DataFrame(
{
"foo1": [10, 99, 13],
"foo": [2, 5, 7]
}
)

res = df_2.max(axis=None, numeric_only=True)
exp = 99
assert res == exp

# DataFrame consists of ints
df_3 = DataFrame(
{
"foo1": [1, 10, 15],
"foo": [4, 5, 6]
}
)

res = df_3.min(axis=None, numeric_only=True)
exp = 1
assert res == exp

# DataFrame consists of ints
df_4 = DataFrame(
{
"foo1": [10, 99, 13],
"foo": [2, 5, 7]
}
)

res = df_4.min(axis=None, numeric_only=True)
exp = 2
assert res == exp

def test_mean_int(self):
"""Testing method mean."""
# DataFrame consists of ints
df_1 = DataFrame(
{
"foo1": [1, 10, 15],
"foo": [4, 5, 6]
}
)

res = df_1.mean(axis=None, numeric_only=True)
exp = 41 / 6
assert res == exp

# DataFrame consists of ints
df_2 = DataFrame(
{
"foo1": [10, 99, 13],
"foo": [2, 5, 7]
}
)

res = df_2.mean(axis=None, numeric_only=True)
exp = 136 / 6
assert res == exp

def test_median_int(self):
"""Testing method median."""
# DataFrame consists of ints
df_1 = DataFrame(
{
"foo1": [1, 10, 15],
"foo": [4, 5, 6]
}
)

res = df_1.median(axis=None, numeric_only=True)
exp = 5.5
assert res == exp

# DataFrame consists of ints
df_2 = DataFrame(
{
"foo1": [10, 99, 13],
"foo": [2, 5, 7],
"foo2": [1, 6, 5]
}
)

res = df_2.median(axis=None, numeric_only=True)
exp = 6
assert res == exp